<meta http-equiv="X-UA-Compatible" content="IE=10" />
<meta content="text/html; charset=UTF-8; X-Content-Type-Options=nosniff" http-equiv="Content-Type"/>
<style>	
	.drive_panel_resize{cursor: pointer;}
	.drive_panel_functions{width: 100%;border-bottom: solid 1px white;display: none}
    .drive_fixedwidth{width: 90px !important; overflow: hidden; height: 16px !important;white-space:nowrap;text-overflow:ellipsis;background: #fff;color:#000;}
    .drive_controlIcon{background:url("imgs/dropdownBlack.png")!important;background-repeat: no-repeat;width: 1px!important;height: 3px;position: relative;top: 1px;left: 38px}
    .drive_filter_icon{background:url("imgs/dropdownBlack.png")!important;background-repeat: no-repeat;width: 1px;height: 3px;position: relative;top: 6px;left: -4px}
    .drive_contact{border-bottom: solid 1px white;float: left;overflow: hidden;}
    .drive_file_name{display: block;white-space: nowrap;text-overflow:ellipsis;-o-text-overflow:ellipsis;overflow: hidden;line-height: 20px;max-width: 148px;font-size: 14px;}
    .drive_file_last_modified{display: block;white-space: nowrap;text-overflow:ellipsis;-o-text-overflow:ellipsis;overflow: hidden;line-height: 20px;color: rgb(165,165,165);}
    .drive_info{background: url(imgs/drive_info.png);}
    .drive_more{position:fixed;bottom: 10px;left: 99px;display: none;}
    .drive_sync{position:fixed;bottom: 20px;left: 99px;display: none;}
    .moreaa{position:fixed;bottom: 10px;left: 99px;display: block;}
    .textUndreline{text-decoration: underline;}
    .symbol{cursor: default!important;}
    .bg_gray{background-color: #76db3d!important}
    #drive_panel_header{background: #8ab503;color: #fff;height: 20px;line-height: 20px;text-align: center;font-size: 14px;}
    #drive_menubar_filter {color: black;}
	#drive_menubar_filter .k-animation-container{width: 94px !important;margin:2px 0px 0px 0px !important;}
 	#drive_menubar_filter .k-animation-container ul{ width: 94px !important;}
	#drive_menubar_filter{position: absolute;z-index: 99999998;}
    #drive_upload{width: 10px;height: 10px;background: url(imgs/drive_upload.png);float: right;position: relative;top: 3px;margin-right: 18px;cursor: pointer;}
    #drive_trash{width: 10px;height: 10px;background: url(imgs/drive_trash.png);float: right;position: relative;top: 3px;margin-right: 18px;}
    #drive_search_min{width: 9px;height: 10px;background: url(imgs/drive_search_min.png);float: right;position: relative;top: 3px;margin-right: 18px;cursor: pointer;background-repeat: no-repeat;}
    #drive_googleDrive{width: 15px;height: 14px;background: url(imgs/drive_googleDrive.png);float: right;position: relative;top: 1px;margin-right: 8px;cursor: pointer;}
    #filePicker{width: 12px;height: 12px;float: right;position: relative;left: 10px;top: 1px;opacity: 0;overflow: hidden;cursor: pointer;display: none;}
    #drive_filterMenu > .k-state-hover,.k-state-hover:hover{background-color:#0af;}
    #drive_filterMenu li span {padding-left: 4px;}
    #wp_google_drive_details_icon{position: fixed; background-color:#ffffff; display: none; height: 0px; line-height: 11px;z-index: 9999999999999999}
    #wp_google_drive_details_content{position: fixed; background-color:#ffffff; display: none; font-size: 12px;}
    #wp_google_drive_details_content .titleDiv{font-weight: bold; font-size: 14px; margin: 10px; width: 100%; height: 17px;}
    #wp_google_drive_details_content .clearStyle{clear: both;}
    #wp_google_drive_details_content hr{height: 1px; border: 0; background-color: #F4F4F4;}
    #wp_google_drive_details_content td{padding-top: 5px;}
    #login_div .urltext{text-align: center;position: absolute;margin: 0 auto;bottom: 0px;width: 100%;}
    #drive_min_panel_div .authorize{text-align: center;position: relative;}   
    #drive_authorize {position: relative;top: 2px;} 
    #drive_nofile{position: absolute;display: none;width: 90%;line-height: 20px;font-size: 16px;text-align: center;}
    #folder_path{display: none;position: relative;float: left;top:2px;font-size: 12px;cursor: pointer;padding-left: 4px;}
    #google_drive_text{display: none;position: relative;float: left;top:2px;font-size: 12px;padding-left: 19px;cursor: default;}
    #folder_path > span {cursor: pointer;}
    #folder_path > span:last-child{cursor: default!important;}
    #drive_filterMenu_button{display: none;margin-left: 20px; width: 600px;overflow: hidden;position: relative;top: -2px;}
    #drive_filterMenu_button button{background-color:gray;height:20px;font-size:12px;border-radius:0;padding:0;font-family:'Arial','sans-serif';color:#fff;line-height: 0;border:none;margin-right: 10px;cursor: pointer;}
    #wp_google_drive_details_img_icon{z-index: 9999999999999999;width: 7px;height: 15px;}
    #drive_search_max_panel{width:355px;height: 20px;display: none;position: relative;left: 405px;margin-top: 8px;}
    #search_max_input{width: 335px;height:20px;border:none;background: url(imgs/drive_search_input.png);font-size: 12px;float: left;text-indent: 5px;}
    #search_max_input_button{width: 20px;height: 20px;background: url(imgs/drive_input_search_max_button.png);float: right;cursor: pointer;}
    .drive_contact a,a:hover{text-decoration: none;color: black;}
    .drive_googleDrive_filePicker_position{top: -18px!important;}
    .drive_trash_upload_position{top:-16px!important;}
    .highlight{color: red}
    #timeout_message{width: 90%;line-height: 20px;font-size: 16px;text-align: center;top: 54%;position: relative;}
    .search_result_text{width: 665px;cursor: text!important;position: relative;top: -15px;left: 13px;white-space: nowrap;text-overflow:ellipsis;-o-text-overflow:ellipsis;overflow: hidden;}
    .mouse_pointer{cursor: pointer!important;}
    #drive #search_no_result{color: red;}
    .drive_contact > div > input[type=checkbox]{float: left;margin-top: 17px}
</style>
<style type="text/css" id="commloader_position"></style>
<div id="driveTpl">
	<div id="drive_panel_header">Google Drive
		<i class="main_common_f_13_13 drive_panel_resize right wp_mt_3 wp_mr_5" title="Expand" onclick=''></i>
	</div>
    <div class="drive_panel_functions wp_h_26 wp_pt_10">
            <div id="drive_filterMenu" class="left wp_ml_17">
                <ul id="drive_menubar_filter" class="wp_fz_10 wp_h_16 wp_w_82"></ul>
            </div>
            <div id="drive_filterMenu_button">
                <button class='wp_w_100' id="my_drive_button" >My Drive</button>
                <button class='wp_w_100' id="share_with_me_button">Shared with Me</button>
                <button class='wp_w_100' id="receen_button">Recent</button>
                <button class='wp_w_100' id="starred_button">Starred</button>
                <button class='wp_w_100' id="trash_button">Trash</button>
            </div>
            <div id="drive_googleDrive" title="Open Drive in New tab"></div>
            <div id="drive_search_min" title="Search"></div>
            <div id="drive_trash" title="Delete" style="opacity:0.6"></div>
            <div id="drive_upload" title="Upload"></div>
            <input id="filePicker" type="file" title="Upload" multiple >
            <div id="drive_search_max_panel">
                <input id="search_max_input" type='text' onkeypress="drive.searchbtnClick(event)" />
                <div id="search_max_input_button" title="Search" ></div>
            </div>
            <div id="google_drive_text">Google Drive</div> 
            <div id="folder_path"></div> 
    </div>
    <div id="drive_min_panel_div" data-template="drive_min_panel_list" data-bind="source: this">
        <div class="top authorize" style="display: none; height: 100%;overflow:hidden" id="login_div">
            <img src="imgs/drive_signin.png"/>
            <button type="button" class="k-button" id="drive_authorize"> Sign in </button>
            <div class="urltext wp_fz_12">www.docs.google.com</div>
        </div>
    </div>
</div>
<div id='wp_google_drive_details_content' data-template="drive_min_details" data-bind="source: this" class="drive_details_content"></div>

<script id="drive_min_panel_list" type="text/x-kendo-template">
<div>
<div id='wp_google_drive_details_icon' class="drive_details_icon_triangle">
    <img id="wp_google_drive_details_img_icon" src="imgs/drive_triangle.png">
</div>
    # for(var i=0, l = data.length; i < l ;i++){ #
        #var openlink = data[i].defaultOpenWithLink#
        #if(openlink == undefined){#
                #openlink = data[i].alternateLink;#
        #}#
        #if(typeof(data[i].sharedWithMeDate) == 'string'){#
            #var _file_type = 'share';#
        #}else{#
            #var _file_type = 'mydrive';#
        #}#
        <div class="left wp_h_60 wp_pl_10 wp_mb_24 wp_mt_2 wp_w_245 drive_contact"  id="#: data[i].id #">
            <div class="left wp_h_45 wp_w_61 wp_mt_6"> 
                <input type="checkbox" class='#:_file_type#' id="#:data[i].id#" data-bind="click:check" name="#:data[i].mimeType#">
                    #if(data[i].mimeType != 'application/vnd.google-apps.folder'){#
                        <label for="#:data[i].id#"><a href = '#:openlink#' target="_blank"><img src="#:data[i].iconLink#" class="left wp_pt_16 wp_pl_14 wp_w_16 wp_h_16" data-listid="#: data[i].id #" style="cursor:pointer;border:none"></a></label>
                    #}else{#
                        <label for="#:data[i].id#"><img src="#:data[i].iconLink#" class="left wp_pt_16 wp_pl_14 wp_w_16 wp_h_16" data-bind="click: abc" data-listid="#: data[i].id #" style="cursor:pointer"></label>
                    #}#
            </div>
            <div id="drive_contact_info" class="left wp_h_40 wp_w_149 wp_mt_9">
                #if(data[i].mimeType != 'application/vnd.google-apps.folder' ){#
                    <a class="drive_file_name left wp_lh_20 wp_fz_12 wp_h_20" href="#:openlink#" data-listid="#: data[i].id #"style="cursor:pointer" title="#:data[i].title#" target="_blank">#:data[i].title#</a>
                #}else{#
                    <p class="drive_file_name left wp_lh_20 wp_fz_12 wp_h_20" data-bind="click: abc" data-listid="#: data[i].id #"style="cursor:pointer; border:none" title="#:data[i].title#">#:data[i].title#</p>
                #}#

                #var time = data[i].lastViewedByMeDate #
                # if(time == '1970-01-01T00:00:00.000Z' || time == undefined ){#
                    <p class="drive_file_last_modified left wp_lh_20 wp_fz_12 wp_w_148 wp_h_20">Opened by #:data[i].lastModifyingUserName#</p>
                #}else{#
                    <p class="drive_file_last_modified left wp_lh_20 wp_fz_12 wp_w_148 wp_h_20">Opened by #:data[i].lastModifyingUserName#:#= kendo.toString(new Date(data[i].lastViewedByMeDate), 'd MMMM') #</p>
                #}#
            </div>
            <div class="drive_info right wp_w_15 wp_h_15 wp_mt_21 wp_mr_10" data-bind="events:{mouseover:drive_info_show,mouseleave:drive_info_hide,mouseout:drive_info_hide}" data-listid="#: data[i].id #"></div>
        </div>
    # } #
    <button class='drive_more' class='wp_w_60' data-bind="click:onMore">More</button>
    <button class='drive_sync' class='wp_w_60' data-bind="click:onSync">Sync</button>
</div>  
</script>

<script id="drive_min_details" type="text/x-kendo-template">
 # var _icon_status = $(".drive_details_content").css('display') #
 #if(_icon_status != 'none'){$('.drive_details_icon_triangle').show()}#
# if(filedata.mimeType.indexOf('image') > -1){ #
    #var fileType = 'Image'#
# }else if(filedata.mimeType.indexOf('folder') > -1){ #
    #var fileType = 'Folder'#
#}else if(filedata.mimeType.indexOf('document') > -1){ #
    #var fileType = 'Document'#
    #if(filedata.mimeType == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'){#
        #fileType = 'Excel'#
    #}#
#}else{#
    #var fileType = ''#
#} #
        <div class="titleDiv">Details</div>
        <hr>
        <div class="titleDiv">General Info</div>
        <table class='wp_ml_10' style="margin-right:30px;">
            #if(fileType == 'Image' || fileType == 'Folder' || fileType == 'Document' || fileType == 'Excel'){#
                <tr>
                    <td>Type</td>
                    <td>#:fileType#</td>
                </tr>
            #}else{#
                <tr>
                    <td>Type</td>
                    <td>#:filedata.mimeType#</td>
                </tr>
            #}#
            #if(fileType == 'Image'){#
                <tr>
                    <td>Dimensions&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
                    <td>#:filedata.imageMediaMetadata.width#x#:filedata.imageMediaMetadata.height#</td>
                </tr>
            #}#
            #if(fileType != 'Folder'){#
                #if(filedata.fileSize != undefined){#
                    # fileSize = filedata.fileSize/1000 #
                    <tr>
                        <td>Size</td>
                        <td>#:fileSize.toFixed(2)#KB</td>
                    </tr>
                #}#  
            #}#
             #if(filedata.modifiedDate != undefined && filedata.modifiedDate != '1970-01-01T00:00:00.000Z' ){#
                <tr>
                    <td>Modified</td> 
                    <td>#:kendo.toString(new Date(filedata.modifiedDate), 'h:mm tt MMM dd')#</td>
                </tr>
             #}#
            
            #if(filedata.createdDate != undefined && filedata.createdDate != '1970-01-01T00:00:00.000Z' ){#
                <tr>
                    <td>Created</td>
                    <td>#:kendo.toString(new Date(filedata.createdDate), 'h:mm tt MMM dd')#</td>
                </tr>
            #}#
            #if(filedata.lastViewedByMeDate != undefined && filedata.lastViewedByMeDate != '1970-01-01T00:00:00.000Z' ){#
                <tr>
                    <td>Opened by me</td>
                    <td>#:kendo.toString(new Date(filedata.lastViewedByMeDate), 'h:mm tt MMM dd')#</td>
                </tr>
            #}#
        </table>
        #if(filedata.description != undefined){#
            <div class="titleDiv">
                <div class="left">Description</div>
                <hr class='left wp_mt_10 wp_ml_10 wp_w_90' />
            </div>
            <div class='wp_ml_10 wp_w_230' style="word-break:break-all;">#:filedata.description#</div>
        #}#
        <div class="titleDiv clearStyle" >
            <div class='left'>Sharing</div>
            <hr class='left wp_mt_10 wp_ml_10 wp_w_117' />
        </div>
        <div class='clearStyle wp_ml_10'>
            <div class='left'>#:filedata.owners[0].displayName#</div>
            <div class='right wp_mr_10'>Owner</div>
        </div>
        <div class="titleDiv" style="margin-top:38px">
            <div class='left'>Download Permission</div>
            <hr class='left wp_mt_10 wp_ml_10 wp_w_20' />
        </div>
        #if(filedata.result.copyable == false){#
            <div class='wp_ml_10'>Prevent viewers from downloading</div>
        #}else{#
            <div class='wp_ml_10'>Allow viewers from downloading</div>
        #}#
        <div class="wp_h_30"></div>
</script>
<script type="text/javascript" src="js/drive.main.js"></script>
<script>
	var drive = (function () {
        var useragent = Workport.getBrowser();
		var pageTpl = $('#driveTpl');
        var minPageTmpl = $('#drive_min_panel_div');
        var width = $(window).width();
        var height = Screen.bodyHeight;
		var filterMenu_i = 1,filterMenu_menu = [{id:'.v0', val:2},{id:'.v2', val:1},{id:'.v3', val:3},{id:'.v4', val:4},{id:'.v5', val:5}];
		var filterMenu_ds ={
            text:" My Drive",
            value:"2",
            cssClass:'v'+filterMenu_i,
            items:[{text:' My Drive',value:2,cssClass:'.v0'},{text: " Shared with Me", value:"1",cssClass:'.v2'},{text: " Recent", value:"3",cssClass:'.v3'},{text: " Starred", value:"4",cssClass:'.v4'},{text: " Trash", value:"5",cssClass:'.v5'}]
        };
        var commLoader;
        var rootId = undefined;
        var permissionId = undefined;
        commLoader = new Workport.Loader($('#drive'),{ text:'Loading', position:'center',imgColor:'black', textCSS:{'color':'black'} });
        var loader_bg_white = {text:'Loading', position:'center',imgColor:'black', textCSS:{'color':'black'},boxCSS: {'opacity': 1, 'background-color': 'white'}};
        var loader_bg_transparent = {text:'Loading', position:'center',imgColor:'black', textCSS:{'color':'black'},boxCSS: {'opacity': 0, 'background-color': 'white'}};
        var loader_top = {imgColor: 'black', position:'up', text:'',textCSS: {'color': 'white'}, imgCSS: {'margin-top':'60px'}, boxCSS: {'opacity': 0, 'background-color': 'white'}};
        var loader_bottom = {imgColor: 'black', position:'down', text:'',textCSS: {'color': 'black'}, imgCSS: {'margin-bottom':'-10px'}, boxCSS: {'opacity': 0, 'background-color': 'white'}};
        var drive_syncing = false;
        var drive_auto_sync = undefined;
        function onSelect(e) {
            $('#search_max_input').val("");
            clearTimeout(drive_auto_sync);
            $('#google_drive_text').css('color','black');
			var item = $(e.item);
            var _txt = item.find('span').text();
            drive_syncing = true;
            if(_txt.length < 20){
                $('#drive_menubar_filter').find('span:first').html('<span class="k-icon k-i-arrow-s drive_filter_icon right" ></span>'+(_txt == 'My Drive' ? 'My Drive' : _txt));
            }
            var classes = item.attr('class').split(' ');
            var csel = 2;
            if($.inArray('.v0',classes) !== -1){
                minPageTmpl.empty();
                commLoader.setOptions(loader_bg_transparent);
                commLoader.show();
                $('#folder_path').find('span').remove();
                $('#drive_trash').css('opacity','0.6');
                $('#drive_trash').css('cursor','');
                drive.loaddata();                     
            }else if($.inArray('.v2',classes) !== -1){
                minPageTmpl.empty();
                commLoader.setOptions(loader_bg_transparent);
                commLoader.show();
                $('#folder_path').find('span').remove();
                $('#drive_trash').css('opacity','0.6');
                $('#drive_trash').css('cursor','');
                Workport.Google.drive.files.listFull({maxResults:50,'q':'sharedWithMe=true'},function(resp,nextPageToken){
                    if(resp.length == 0){
                        var viewModel = drivelist([],undefined);
                        kendo.bind($("#drive_min_panel_div"), viewModel);
                        $('#drive_min_panel_div').append('<div id="drive_nofile" class="center">Files and Folders, others have shared with you, will appear here.</div>');
                        $('.drive_panel_functions').css('display','block');
                        $('#drive_nofile').css('display','block');
                        commLoader.hide();
                        $('.drive_more').attr('id','sharedWithMe');
                        $('.drive_more').attr('disabled','disabled'); 
                        drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                    }else{
                        var viewModel = drivelist(resp,nextPageToken);
                        kendo.bind($("#drive_min_panel_div"), viewModel);
                        commLoader.hide();
                        drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                        var list_length = resp.length;
                        if(list_length > 0 ){
                            drive.remove_border_bottom();
                        }
                        $('.drive_more').attr('id','sharedWithMe');
                        if(nextPageToken == undefined){
                            $('.drive_more').attr('disabled','disabled'); 
                        }
                    }
                });
            }else if($.inArray('.v3',classes) !== -1){
                minPageTmpl.empty();
                commLoader.setOptions(loader_bg_transparent);
                commLoader.show();
                $('#folder_path').find('span').remove();
                $('#drive_trash').css('opacity','0.6');
                $('#drive_trash').css('cursor','');
                Workport.Google.drive.files.list(function(resp){
                    var listdata_lastview = [];
                    for(m in resp){
                        if(resp[m].lastViewedByMeDate != undefined && resp[m].labels.trashed == false && resp[m].mimeType != 'application/vnd.google-apps.folder'){
                            listdata_lastview.push(resp[m]);
                        }
                    }
                    if(listdata_lastview.length == 0){
                        var viewModel = drivelist([],undefined);
                        kendo.bind($("#drive_min_panel_div"), viewModel);
                        $('#drive_min_panel_div').append('<div id="drive_nofile" class="center">No recent activity.</div>');
                        $('.drive_panel_functions').css('display','block');
                        $('#drive_nofile').css('display','block');
                        commLoader.hide();
                        $('.drive_more').attr('id','recent');
                        $('.drive_more').attr('disabled','disabled');
                        drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                    }else{
                        listdata_lastview = Workport.Google.drive.files.listsort(listdata_lastview);
                        var viewModel = drivelist(listdata_lastview,undefined);
                        kendo.bind($("#drive_min_panel_div"), viewModel);
                        commLoader.hide();
                        drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                        var list_length = listdata_lastview.length;
                        if(list_length > 0 ){
                            drive.remove_border_bottom();
                        }
                        $('.drive_more').attr('id','recent');
                        $('.drive_more').attr('disabled','disabled');
                    }
                });
            }else if($.inArray('.v4',classes) !== -1){
                minPageTmpl.empty();
                commLoader.setOptions(loader_bg_transparent);
                commLoader.show();
                $('#folder_path').find('span').remove();
                $('#drive_trash').css('opacity','0.6');
                $('#drive_trash').css('cursor','');
                Workport.Google.drive.files.listFull({maxResults:50,'q':'starred=true and trashed = false'},function(resp,nextPageToken){
                    if(resp.length == 0){
                        var viewModel = drivelist([],undefined);
                        kendo.bind($("#drive_min_panel_div"), viewModel);
                        $('#drive_min_panel_div').append('<div id="drive_nofile" class="center">Starred files and folders will appear here.</div>');
                        $('.drive_panel_functions').css('display','block');
                        $('#drive_nofile').css('display','block');
                        commLoader.hide();
                        $('.drive_more').attr('id','starred');
                        $('.drive_more').attr('disabled','disabled'); 
                        drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                    }else{
                        resp = Workport.Google.drive.files.listsort(resp);
                        var viewModel = drivelist(resp,nextPageToken);
                        kendo.bind($("#drive_min_panel_div"), viewModel);
                        commLoader.hide();
                        drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                        var list_length = resp.length;
                        if(list_length > 0 ){
                            drive.remove_border_bottom();
                        }
                        $('.drive_more').attr('id','starred');
                        if(nextPageToken == undefined){
                            $('.drive_more').attr('disabled','disabled'); 
                        }
                    }
                });
            }else if($.inArray('.v5',classes) !== -1){
                minPageTmpl.empty();
                commLoader.setOptions(loader_bg_transparent);
                commLoader.show();
                $('#drive_trash').css('opacity','0.6');
                $('#drive_trash').css('cursor','');
                Workport.Google.drive.files.listtrash(function(resp){                
                        var datalist =[];
                        var folderlist = [];
                        var filelist = [];
                        var deletelist = [];
                        
                        for(m in resp){
                            if(resp[m].mimeType == 'application/vnd.google-apps.folder' && resp[m].labels.trashed == true){
                                folderlist.push(resp[m]);
                            }
                        }
                        
                        for(n in resp){
                            if(resp[n].mimeType != 'application/vnd.google-apps.folder' && resp[n].labels.trashed == true){
                                filelist.push(resp[n]);
                            }
                        }

                        datalist = datalist.concat(folderlist);
                        datalist = datalist.concat(filelist);
                      
                       for(i in folderlist){
                            for(j in filelist){
                                if(filelist[j].parents[0].id == folderlist[i].id){
                                    deletelist.push(filelist[j]);
                                }
                            }
                       }
                      
                        for(k in folderlist){
                            for(c in folderlist){
                                if(folderlist[c].parents[0].id == folderlist[k].id){
                                    deletelist.push(folderlist[c]);
                                }
                            }
                        }

                        for(a in deletelist){
                            for(b in datalist){
                                if( datalist[b].id == deletelist[a].id){
                                    datalist.splice(b,1);
                                }
                            }
                        }
                        if(datalist.length == 0){
                        var viewModel = drivelist([],undefined);
                        kendo.bind($("#drive_min_panel_div"), viewModel);
                        $('#drive_min_panel_div').append('<div id="drive_nofile" class="center">No files or folders in Trash.</div>');
                        $('.drive_panel_functions').css('display','block');
                        $('#drive_nofile').css('display','block');
                        commLoader.hide();
                        $('.drive_more').attr('id','trash');
                        $('.drive_more').attr('disabled','disabled');
                        drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                    }else{
                        var viewModel = drivelist(datalist,undefined);
                        kendo.bind($("#drive_min_panel_div"), viewModel);
                        commLoader.hide();
                        drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                        var list_length = datalist.length;
                        if(list_length > 0 ){
                            drive.remove_border_bottom();
                        }
                        $('.drive_more').attr('id','trash');
                        $('.drive_more').attr('disabled','disabled'); 
                    }
                });
            }else{
                 $.map(classes,function(c){
                    if(c.indexOf('.v') === 0){
                        $.map(filterMenu_menu,function(_item){
                            if(c == _item.id){
                                csel =  _item.val;
                            }
                        });
                    }
                });
            }   
        }

        function wp_drive_filterMenu(){
			$("#drive_menubar_filter").kendoMenu({
                dataSource:filterMenu_ds,
                select:onSelect
            });
            if(navigator.userAgent.indexOf("Firefox")>0){
            	$('#drive_menubar_filter').find('span:eq(0)>span').addClass('drive_controlIcon');
                $('drive_menubar_filter .k-icon').css('width','12px');
            }else{
            	$('#drive_menubar_filter').find('span:eq(0)>span').addClass('k-icon k-i-arrow-s drive_filter_icon right ');
            }
            $('#drive_menubar_filter').find('span:eq(0)').addClass('drive_fixedwidth');
           
        }
        
        function getdetails(data){
            var vm = kendo.observable({
                filedata:data
            });
            return vm;
        }

        function drivelist(listdata,nextPageToken){
            var selectist = [];
            var vm = kendo.observable({
                data:listdata,
                nextPageToken:nextPageToken,
                abc:function(e){
                    commLoader.setOptions(loader_bg_transparent);
                    commLoader.show();
                    clearTimeout(drive_auto_sync);
                    var listid = $(e.target).data('listid');
                    var _folderlist_data = [];
                    var _listdata = [];
                    var more_button_id=$('.drive_more').attr('id');

                     Workport.Google.drive.files.get(listid,function(resp){
                        if(resp.error){
                            $('#drive_min_panel_div').empty();
                            $('#drive_min_panel_div').append('<div id="timeout_message" class="center">'+resp.error.message+'</div>');
                            commLoader.hide();
                            $('.drive_panel_functions').hide();
                            return
                        }
                        if(resp.mimeType == 'application/vnd.google-apps.folder' ){
                                if(more_button_id == 'trash'){
                                    Workport.utils.alertMsg("Confirmation alert", "This folder is in your trash.", function(_data){commLoader.hide();});
                                }else{
                                    $('#drive_min_panel_div').empty();
                                    if($('#google_drive_text').text() != 'Back to all'){
                                        Workport.Google.drive.files.get(listid,function(_resp){
                                            $('#folder_path').append('<span class="symbol"> > </span>')
                                            $('#folder_path').append('<span '+'id = '+'"'+_resp.id+'"' +'>' + _resp.title + '</span>');
                                            $('#'+_resp.id).prevAll('span').addClass('textUndreline');
                                            $('.symbol').removeClass('textUndreline');
                                            $('#google_drive_text').addClass('mouse_pointer');
                                            $('#google_drive_text').css('color','blue');
                                        });
                                    }
                                    Workport.Google.drive.files.listFullfolderfiles({maxResults:10000,'q':'mimeType = "application/vnd.google-apps.folder" and trashed = false'},listid,function(resp,nextPageToken){
                                        for(i in resp){
                                                if(resp[i].parents[0] != undefined){
                                                    _folderlist_data.push(resp[i]);
                                                } 
                                        }
                                        _folderlist_data = Workport.Google.drive.files.listsort(_folderlist_data);
                                        Workport.Google.drive.files.listFullfolderfiles({maxResults:70,'q':'mimeType != "application/vnd.google-apps.folder" and trashed = false'},listid,function(resp,nextPageToken){
                                            for(i in resp){
                                                if(resp[i].parents[0] != undefined){
                                                    _listdata.push(resp[i]);
                                                } 
                                            }
                                            if(_folderlist_data.length == 0 && _listdata.length ==0){
                                                var viewModel = drivelist([],undefined);
                                                kendo.bind($("#drive_min_panel_div"), viewModel);
                                                $('#drive_min_panel_div').append('<div id="drive_nofile" class="center">This folder is empty. You can use the upload button on top of this panel to upload files.</div>');
                                                $('.drive_panel_functions').css('display','block');
                                                $('#drive_nofile').css('display','block');
                                                commLoader.hide();
                                                if(more_button_id == 'sharedWithMe'){
                                                    $('.drive_sync').attr('id',more_button_id);
                                                }
                                                $('.drive_more').attr('id',listid);
                                                $('.drive_more').attr('disabled','disabled');
                                                drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                            }else{
                                                _listdata = Workport.Google.drive.files.listsort(_listdata);
                                                _folderlist_data = _folderlist_data.concat(_listdata);
                                                var viewModel = drivelist(_folderlist_data,nextPageToken);
                                                $('.drive_panel_functions').css('display','block');
                                                kendo.bind($("#drive_min_panel_div"), viewModel);
                                                commLoader.hide();
                                                drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                                var list_length = _folderlist_data.length;
                                                if(list_length > 0 ){
                                                    drive.remove_border_bottom();
                                                }
                                                if(more_button_id == 'sharedWithMe'){
                                                    $('.drive_sync').attr('id',more_button_id);
                                                }
                                                $('.drive_more').attr('id',listid);
                                                if(nextPageToken == undefined){
                                                   $('.drive_more').attr('disabled','disabled'); 
                                                }
                                            }
                                            
                                        });
                                    })
                                }
                        }else{
                                openlink = resp.defaultOpenWithLink;
                                if(openlink == undefined){
                                    openlink = resp.alternateLink;
                                }
                                commLoader.hide();
                                drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                window.open(openlink);
                        }
                     })
                },
                check:function(){
                    var counter = 0;
                    $('#drive_min_panel_div').find(":checkbox").each(function(){
                        if(this.checked == true){
                            counter++
                        }
                     })
                    if(counter == 0){
                        $('#drive_trash').css('opacity','0.6');
                        $('#drive_trash').css('cursor','');
                    }else{
                        $('#drive_trash').css('opacity','1');
                        $('#drive_trash').css('cursor','pointer');
                    }
                },
                drive_info_show:function(e){
                    var listid = $(e.target).data('listid');
                    Workport.Google.drive.files.get(listid,function(resp){
                        var viewModel = getdetails(resp);
                        kendo.bind($("#wp_google_drive_details_content"), viewModel);
                     })
                    var google_Drive_Details = function () {
                        var contentElem = $("#wp_google_drive_details_content");
                        var iconElem = $("#wp_google_drive_details_icon");
                        return {
                            showDetails: function (_options, _constent) {
                                var left_icon = $(_options.elem).position().left + $(_options.elem).width(), top = $(_options.elem).position().top, contentTop, picTop, detailHeight = contentElem.height(), picHeight = iconElem.height(),left = $(_options.elem).offset().left + $(_options.elem).width();
                                var _window_statu = $('#drive_filterMenu_button').css('display');
                                if(detailHeight == 0){
                                    detailHeight = 200;
                                }
                                top = top < _options.topOffset ? _options.topOffset : top + _options.topOffset;
                                contentTop = top + detailHeight > _options.winHeight ? _options.winHeight - detailHeight + _options.bottomOffset : top;
                                picTop = top + picHeight > _options.winHeight ? _options.winHeight - picHeight + _options.bottomOffset : top;                           
                                contentElem.css({'top': contentTop, 'left': left + iconElem.width()});
                                if(useragent == 'ie11' || useragent == 'ie10'){
                                    if(_window_statu == 'block'){
                                        iconElem.css({'top': picTop+156, 'left': left});
                                    }else{
                                        iconElem.css({'top': picTop+108, 'left': left});
                                    }    
                                }else if(useragent == 'safari_mac' || useragent == 'safari_windows'){
                                    if(_window_statu == 'block'){
                                        iconElem.css({'top': picTop-5, 'left': left_icon});
                                    }else{
                                        iconElem.css({'top': picTop-5, 'left': left_icon});
                                    }
                                }else{
                                    if(_window_statu == 'block'){
                                        iconElem.css({'top': picTop+92, 'left': left_icon});
                                    }else{
                                        iconElem.css({'top': picTop+50, 'left': left_icon});
                                    } 
                                }
                                contentElem.show();
                            }
                        }
                    }();
                    google_Drive_Details.showDetails({elem:$('#'+listid), winHeight: $('#drive_min_panel_div').height() + 30, 'topOffset': 31, bottomOffset: 10});
                },
                drive_info_hide:function(){
                    $("#wp_google_drive_details_icon").hide();
                    $("#wp_google_drive_details_icon").css('display','none');
                    $("#wp_google_drive_details_content").empty();
                    $("#wp_google_drive_details_content").hide();                
                },
                onMore: function (e) {
                    var _path = $('.drive_more').attr('id');
                    var status = $('.drive_more').attr('disabled')
                    var self = this;
                    var more_button_id=$('.drive_more').attr('id');
                    clearTimeout(drive_auto_sync);
                    if(_path == 'trash' || _path == 'starred' || _path == 'sharedWithMe'){
                        if(status == undefined && _path == 'starred'){
                            drive.getMoreStarred(this.get('nextPageToken'),function(list, nextPageToken) {
                                if(list.length > 0){
                                    listdata = listdata.concat(list);
                                    vm.set('data', listdata);
                                    vm.set('nextPageToken', nextPageToken);
                                    var list_length = list.length;
                                    drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                    if(list_length > 0 ){
                                        drive.remove_border_bottom();
                                    }
                                    $('.drive_more').attr('id',_path);
                                    if(nextPageToken == undefined){
                                        $('.drive_more').attr('disabled','disabled');
                                    }
                                }else{
                                    drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                }
                            });
                        }else if(status == undefined && _path == 'sharedWithMe'){
                            drive.getMoreSharedWithMe(this.get('nextPageToken'),function(list, nextPageToken) {
                                if(list.length > 0 ){
                                    listdata = listdata.concat(list);
                                    vm.set('data', listdata);
                                    vm.set('nextPageToken', nextPageToken);
                                    var list_length = list.length;
                                    drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                    if(list_length > 0 ){
                                        drive.remove_border_bottom();
                                    }
                                    $('.drive_more').attr('id',_path);
                                    if(nextPageToken == undefined){
                                        $('.drive_more').attr('disabled','disabled');
                                    }
                                }else{
                                    drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                }
                            });
                        }

                    }else{
                        if(status == undefined){
                                drive.getMorefolderfile(this.get('nextPageToken'),more_button_id,function(list, nextPageToken) {
                                    if(list.length > 0 ){
                                        listdata = listdata.concat(list);
                                        vm.set('data', listdata);
                                        vm.set('nextPageToken', nextPageToken);
                                        var list_length = list.length;
                                        if(list_length > 0 ){
                                            drive.remove_border_bottom();
                                        }
                                         $('.drive_more').attr('id',more_button_id);
                                         if(nextPageToken == undefined){
                                             $('.drive_more').attr('disabled','disabled');         
                                        }
                                         drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                    }else{
                                        drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                    }
                                });
                        }
                        drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                    }
                },
                onSync:function(){
                    clearTimeout(drive_auto_sync);
                    var more_button_id=$('.drive_more').attr('id');
                    var more_button_status = $('.drive_more').attr('disabled');
                    commLoader.setOptions(loader_top);
                    commLoader.show();
                    var _syncdata = [];
                    var _folder = [];
                    var _file = [];
                    var counter = 0;
                        if(more_button_id == 'search_result'){
                            commLoader.hide();
                            return
                        }
                        if(more_button_id == 'starred'){ 
                                var _sync_starred = [];
                                var _starred_counter = 0;
                                if(listdata.length == 0 ){
                                    Workport.Google.drive.files.listFull({maxResults:50,'q':'starred=true and trashed = false'},function(resp,nextPageToken){
                                        if(resp.length == 0){
                                            minPageTmpl.empty();
                                            var viewModel = drivelist([],undefined);
                                            kendo.bind($("#drive_min_panel_div"), viewModel);
                                            drive.remove_border_bottom();
                                            commLoader.hide();
                                            $('#drive_min_panel_div').append('<div id="drive_nofile" class="center">Starred files and folders will appear here.</div>');
                                            $('.drive_panel_functions').css('display','block');
                                            $('#drive_nofile').css('display','block');
                                            $('.drive_more').attr('id','starred');
                                            $('.drive_more').attr('disabled','disabled'); 
                                            drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                        }else{
                                            resp = Workport.Google.drive.files.listsort(resp);
                                            var viewModel = drivelist(resp,nextPageToken);
                                            minPageTmpl.empty();
                                            kendo.bind($("#drive_min_panel_div"), viewModel);
                                            commLoader.hide();
                                            drive.remove_border_bottom();
                                            drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                            $('.drive_more').attr('id','starred');
                                            if(nextPageToken == undefined){
                                                $('.drive_more').attr('disabled','disabled'); 
                                            }
                                        }
                                    });
                                }else{
                                    for(i in listdata){
                                        Workport.Google.drive.files.get(listdata[i].id,function(resp){
                                            if(resp.error){
                                                $('#drive_min_panel_div').empty();
                                                $('#drive_min_panel_div').append('<div id="timeout_message" class="center">'+resp.error.message+'</div>');
                                                commLoader.hide();
                                                $('.drive_panel_functions').hide();
                                                return
                                            }
                                            if(resp.labels.starred == true && resp.labels.trashed == false){
                                                _sync_starred.push(resp);
                                            }
                                            _starred_counter++
                                            if(_starred_counter == listdata.length){
                                                if(_sync_starred.length > 0 ){
                                                    var data_one_starred = _sync_starred[0].modifiedDate;
                                                    for(m in _sync_starred){
                                                       if(_sync_starred[m].modifiedDate > data_one_starred){
                                                            data_one_starred = _sync_starred[m].modifiedDate;
                                                       }
                                                    }
                                                    Workport.Google.drive.files.listFull({'q':'modifiedDate  >'+'"'+data_one_starred+'"'+'and trashed = false and starred=true'},function(resp,nextPageToken){
                                                            if(resp.length == '0' && listdata.length == _sync_starred.length){
                                                                commLoader.hide();
                                                                drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                                                return
                                                            }else{
                                                                resp = Workport.Google.drive.files.listsort(resp);
                                                                _sync_starred = Workport.Google.drive.files.listsort(_sync_starred);
                                                                _sync_starred = resp.concat(_sync_starred);
                                                                vm.set('data', _sync_starred);
                                                                drive.remove_border_bottom();
                                                                _sync_starred = Workport.Google.drive.files.listsort(_sync_starred);
                                                                listdata = _sync_starred;
                                                                $('.drive_more').attr('id',more_button_id);
                                                                if(more_button_status == 'disabled'){
                                                                     $('.drive_more').attr('disabled','disabled');
                                                                }
                                                                commLoader.hide();
                                                                drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                                                return
                                                            }
                                                    })
                                                }else{
                                                    Workport.Google.drive.files.listFull({maxResults:50,'q':'starred=true and trashed = false'},function(resp,nextPageToken){
                                                        if(resp.length == 0 ){
                                                            minPageTmpl.empty();
                                                            var viewModel = drivelist([],undefined);
                                                            kendo.bind($("#drive_min_panel_div"), viewModel);
                                                            $('#drive_min_panel_div').append('<div id="drive_nofile" class="center">Starred files and folders will appear here.</div>');
                                                            $('.drive_panel_functions').css('display','block');
                                                            $('#drive_nofile').css('display','block');
                                                            commLoader.hide();
                                                            $('.drive_more').attr('id','starred');
                                                            $('.drive_more').attr('disabled','disabled'); 
                                                            drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                                        }else{
                                                            resp = Workport.Google.drive.files.listsort(resp);
                                                            var viewModel = drivelist(resp,nextPageToken);
                                                            kendo.bind($("#drive_min_panel_div"), viewModel);
                                                            commLoader.hide();
                                                            drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                                            var list_length = resp.length;
                                                            if(list_length > 0 ){
                                                                drive.remove_border_bottom();
                                                            }
                                                            $('.drive_more').attr('id','starred');
                                                            if(nextPageToken == undefined){
                                                                $('.drive_more').attr('disabled','disabled'); 
                                                            }
                                                        }
                                                        
                                                    });
                                                }
                                            }
                                        })
                                    }
                                }  
                        } 
                    if(more_button_id == 'trash'){
                            Workport.Google.drive.files.listtrash(function(resp){
                            var datalist =[];
                            var folderlist = [];
                            var filelist = [];
                            var deletelist = [];
                            
                            for(m in resp){
                                if(resp[m].mimeType == 'application/vnd.google-apps.folder' && resp[m].labels.trashed == true){
                                    folderlist.push(resp[m]);
                                }
                            }
                            
                            for(n in resp){
                                if(resp[n].mimeType != 'application/vnd.google-apps.folder' && resp[n].labels.trashed == true){
                                    filelist.push(resp[n]);
                                }
                            }

                            datalist = datalist.concat(folderlist);
                            datalist = datalist.concat(filelist);
                          
                           for(i in folderlist){
                                for(j in filelist){
                                    if(filelist[j].parents[0].id == folderlist[i].id){
                                        deletelist.push(filelist[j]);
                                    }
                                }
                           }
                          
                            for(k in folderlist){
                                for(c in folderlist){
                                    if(folderlist[c].parents[0].id == folderlist[k].id){
                                        deletelist.push(folderlist[c]);
                                    }
                                }
                            }

                            for(a in deletelist){
                                for(b in datalist){
                                    if( datalist[b].id == deletelist[a].id){
                                        datalist.splice(b,1);
                                    }
                                }
                            }
                            if(datalist.length == 0){
                                minPageTmpl.empty();
                                var viewModel = drivelist([],undefined);
                                kendo.bind($("#drive_min_panel_div"), viewModel);
                                $('#drive_min_panel_div').append('<div id="drive_nofile" class="center">No files or folders in Trash.</div>');
                                $('.drive_panel_functions').css('display','block');
                                $('#drive_nofile').css('display','block');
                                commLoader.hide();
                                $('.drive_more').attr('id','trash');
                                $('.drive_more').attr('disabled','disabled');
                                drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                            }else{
                                minPageTmpl.empty();
                                var viewModel = drivelist(datalist,undefined);
                                kendo.bind($("#drive_min_panel_div"), viewModel);
                                commLoader.hide();
                                drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                var list_length = datalist.length;
                                if(list_length > 0 ){
                                    drive.remove_border_bottom();
                                }
                                $('.drive_more').attr('id','trash');
                                $('.drive_more').attr('disabled','disabled');
                            }  
                        });
                        return    
                    }

                    if(more_button_id == 'recent'){
                        Workport.Google.drive.files.list(function(resp){
                                var listdata_lastview = [];
                                for(m in resp){
                                    if(resp[m].lastViewedByMeDate != undefined && resp[m].labels.trashed == false && resp[m].mimeType != 'application/vnd.google-apps.folder'){
                                        listdata_lastview.push(resp[m]);
                                    }
                                }
                                if(listdata_lastview.length == 0 ){
                                    minPageTmpl.empty();
                                    var viewModel = drivelist([],undefined);
                                    kendo.bind($("#drive_min_panel_div"), viewModel);
                                    commLoader.hide();
                                    $('#drive_min_panel_div').append('<div id="drive_nofile" class="center">No recent activity.</div>');
                                    $('.drive_panel_functions').css('display','block');
                                    $('#drive_nofile').css('display','block');
                                    $('.drive_more').attr('id','recent');
                                    $('.drive_more').attr('disabled','disabled');
                                    drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                }else{
                                    listdata_lastview = Workport.Google.drive.files.listsort(listdata_lastview);
                                    var viewModel = drivelist(listdata_lastview,undefined);
                                    minPageTmpl.empty();
                                    kendo.bind($("#drive_min_panel_div"), viewModel);
                                    commLoader.hide();
                                    drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                    var list_length = listdata_lastview.length;
                                    if(list_length > 0 ){
                                        drive.remove_border_bottom();
                                    }
                                    $('.drive_more').attr('id','recent');
                                    $('.drive_more').attr('disabled','disabled');
                                }
                        });
                        return
                    }

                    if(listdata.length > 0 ){
                        for( m in listdata){
                            Workport.Google.drive.files.get(listdata[m].id,function(resp){
                                if(resp.error){
                                    $('#drive_min_panel_div').empty();
                                    $('#drive_min_panel_div').append('<div id="timeout_message" class="center">'+resp.error.message+'</div>');
                                    commLoader.hide();
                                    $('.drive_panel_functions').hide();
                                    return
                                }
                                if(resp.code != '404' && resp.labels.trashed == false){
                                    _syncdata.push(resp);
                                }
                                counter++
                                    if(counter == listdata.length){
                                        if(_syncdata.length > 0 ){
                                                var data_one = _syncdata[0].modifiedDate;
                                                for(m in _syncdata){
                                                   if(_syncdata[m].modifiedDate > data_one){
                                                        data_one = _syncdata[m].modifiedDate;
                                                   }
                                                }
                                                if(more_button_id =='sharedWithMe'){
                                                        Workport.Google.drive.files.listFull({'q':'modifiedDate  >'+'"'+data_one+'"'+'and trashed = false and sharedWithMe=true'},function(resp,nextPageToken){
                                                                if(resp.length == '0' && listdata.length == _syncdata.length){
                                                                    drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                                                    commLoader.hide();
                                                                    return
                                                                }else{
                                                                    resp = Workport.Google.drive.files.listsort(resp);
                                                                    _syncdata = Workport.Google.drive.files.listsort(_syncdata);
                                                                    _syncdata = resp.concat(_syncdata);
                                                                    vm.set('data', _syncdata);
                                                                    drive.remove_border_bottom();
                                                                    commLoader.hide();
                                                                    drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                                                    _syncdata = Workport.Google.drive.files.listsort(_syncdata);
                                                                    listdata = _syncdata;
                                                                    $('.drive_more').attr('id',more_button_id);
                                                                    if(more_button_status == 'disabled'){
                                                                         $('.drive_more').attr('disabled','disabled');
                                                                    } 
                                                                }
                                                        });
                                                }else{
                                                    Workport.Google.drive.files.listFullfolderfiles({'q':'modifiedDate  >'+'"'+data_one+'"'+'and trashed = false'},more_button_id,function(resp,nextPageToken){
                                                            if(resp.length == '0' && listdata.length == _syncdata.length){
                                                                drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                                                commLoader.hide();
                                                                return
                                                            }else{
                                                                resp = Workport.Google.drive.files.listsort(resp);
                                                                _syncdata = Workport.Google.drive.files.listsort(_syncdata);
                                                                _syncdata = resp.concat(_syncdata);
                                                                vm.set('data', _syncdata);
                                                                drive.remove_border_bottom();
                                                                drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                                                _syncdata = Workport.Google.drive.files.listsort(_syncdata);
                                                                listdata = _syncdata;
                                                                $('.drive_more').attr('id',more_button_id);
                                                                if(more_button_status == 'disabled'){
                                                                     $('.drive_more').attr('disabled','disabled');
                                                                }
                                                                commLoader.hide();
                                                            }
                                                        })
                                                }
                                        }else{
                                            if(more_button_id =='sharedWithMe'){
                                                Workport.Google.drive.files.listFull({'q':'trashed = false and sharedWithMe=true'},function(resp,nextPageToken){
                                                        if(resp.length == '0'){
                                                            vm.set('data', resp);
                                                            $('#drive_min_panel_div').append('<div id="drive_nofile" class="center">Files and Folders, others have shared with you, will appear here.</div>');
                                                            $('.drive_more').attr('id',more_button_id);
                                                            listdata = resp;
                                                            drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                                            commLoader.hide();
                                                            $('#drive_nofile').css('display','block');
                                                            return
                                                        }else{
                                                            resp = Workport.Google.drive.files.listsort(resp);
                                                            vm.set('data', resp);
                                                            drive.remove_border_bottom();
                                                            drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                                            listdata = resp;
                                                            $('.drive_more').attr('id',more_button_id);
                                                            if(more_button_status == 'disabled'){
                                                                 $('.drive_more').attr('disabled','disabled');
                                                            }
                                                            commLoader.hide();
                                                        }
                                                })
                                            }else{
                                                Workport.Google.drive.files.listFullfolderfiles({'q':'trashed = false'},more_button_id,function(resp,nextPageToken){
                                                        if(resp.length == '0'){
                                                            vm.set('data', resp);
                                                            $('#drive_min_panel_div').append('<div id="drive_nofile" class="center">This folder is empty. You can use the upload button on top of this panel to upload files. </div>');
                                                            listdata = resp;
                                                            drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                                            $('.drive_more').attr('id',more_button_id);
                                                            commLoader.hide();
                                                            $('#drive_nofile').css('display','block');
                                                            return
                                                        }else{
                                                            resp = Workport.Google.drive.files.listsort(resp);
                                                            vm.set('data', resp);
                                                            drive.remove_border_bottom();
                                                            drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                                            listdata = resp;
                                                            $('.drive_more').attr('id',more_button_id);
                                                            if(more_button_status == 'disabled'){
                                                                 $('.drive_more').attr('disabled','disabled');
                                                            }
                                                            commLoader.hide();
                                                        }
                                                    })
                                            }
                                        }
                                    }
                            })          
                        }
                    }else{
                        if(more_button_id =='sharedWithMe'){
                            Workport.Google.drive.files.listFull({'q':'trashed = false and sharedWithMe=true'},function(resp,nextPageToken){
                                    if(resp.length == '0'){
                                        drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                        commLoader.hide();
                                        return
                                    }else{
                                        resp = Workport.Google.drive.files.listsort(resp);
                                        minPageTmpl.empty();
                                        vm.set('data', resp);
                                        drive.remove_border_bottom();
                                        drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                        listdata = resp;
                                        $('.drive_more').attr('id',more_button_id);
                                        if(more_button_status == 'disabled'){
                                             $('.drive_more').attr('disabled','disabled');
                                        }
                                        commLoader.hide();
                                    }
                            })
                        }else{
                            Workport.Google.drive.files.listFullfolderfiles({'q':'trashed = false'},more_button_id,function(resp,nextPageToken){
                                    if(resp.length == '0'){
                                        drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                        commLoader.hide();
                                        return
                                    }else{
                                        resp = Workport.Google.drive.files.listsort(resp);
                                        minPageTmpl.empty();
                                        vm.set('data', resp);
                                        drive.remove_border_bottom();
                                        drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                        listdata = resp;
                                        $('.drive_more').attr('id',more_button_id);
                                        if(more_button_status == 'disabled'){
                                             $('.drive_more').attr('disabled','disabled');
                                        }
                                        commLoader.hide();
                                    }
                                })
                        }
                    }      
                }  

            });
            return vm;
        }

        function trashfile() {
             var counter=0;
             var del_file_counter=0;
             var del_folder_counter=0;
             var total_len = 0;
             var folder_len = 0;
             var file_len =0;
             var message = '';
             var trash_message = '';
             clearTimeout(drive_auto_sync);
             $('#drive_min_panel_div').find(":checkbox").each(function(){
                if(this.checked == true){
                    counter++;
                    if(this.name == 'application/vnd.google-apps.folder'){
                        folder_len++;
                    }else{
                        file_len++;
                    }
                }
             });
             if(file_len == 1 && folder_len == 0){
                message = 'Are you sure you want to move the file/folder to Trash?';
             }else if(file_len == 0 && folder_len == 1){
                message = 'Are you sure you want to move the file/folder to Trash?';
             }else if(file_len == 1 && folder_len == 1){
                message = 'Are you sure you want to move 1 file and 1 folder to Trash?';
             }else if(file_len >1 || folder_len >1){
                if(file_len > 1 && folder_len == 0){
                    message = 'Are you sure you want to move '+file_len+' files to Trash?';
                }else if(file_len == 0 && folder_len >1 ){
                    message = 'Are you sure you want to move '+folder_len+' folders to Trash?';
                }else if(file_len > 1 && folder_len == 1){
                    message = 'Are you sure you want to move '+file_len+' files and '+folder_len+' folder to Trash?'; 
                }else if(file_len ==1  && folder_len > 1){
                    message = 'Are you sure you want to move '+file_len+' file and '+folder_len+' folders to Trash?'; 
                }else{
                    message = 'Are you sure you want to move '+file_len+' files and '+folder_len+' folders to Trash?';
                }
             }else{
                message = 'Are you sure you want to trash them ?';
             }
             if(counter>0){
                Workport.utils.alertMsg('Confirmation alert',message,[
                    {name:'Yes',class:'btn yes', css:{}, action:function(e){
                        $(":checkbox").each(function(){
                            if(this.checked == true){
                                var folderid = this.id;
                                Workport.Google.drive.files.trash(folderid,function(resp){
                                    if(resp.id == folderid || resp.code == '404'){
                                        if(resp.mimeType == 'application/vnd.google-apps.folder'){
                                            del_folder_counter++;
                                        }else{
                                            del_file_counter++;
                                        }
                                        total_len++;
                                        $('#'+folderid).remove();
                                    }else{
                                        console.log('not successful');
                                        total_len++;
                                    }
                                    if(total_len == counter){
                                        var list_length = $('.drive_contact').length;
                                        $('#drive_trash').css('opacity','0.6');
                                        $('#drive_trash').css('cursor','');
                                        $('#drive').find('input:checkbox').attr('checked', false);
                                        if(del_file_counter == 0 && del_folder_counter == 0){
                                            trash_message = '0 file/folder have been moved to Trash.';
                                        }else if(del_file_counter == 1 && del_folder_counter == 0){
                                            trash_message = 'The file/folder has been moved to Trash. ';
                                        }else if(del_file_counter == 0 && del_folder_counter == 1){
                                            trash_message = 'The file/folder has been moved to Trash. ';
                                        }else if(del_file_counter == 1 && del_folder_counter == 1){
                                            trash_message = '1 file and 1 folder have been moved to Trash.';
                                        }else if(del_file_counter >1 || del_folder_counter >1){
                                            if(del_file_counter > 1 && del_folder_counter == 0){
                                                trash_message = del_file_counter+' files have been moved to Trash.';
                                            }else if(del_file_counter == 0 && del_folder_counter >1 ){
                                                trash_message = del_folder_counter+' folders have been moved to Trash.';
                                            }else if(del_file_counter > 1 && del_folder_counter == 1){
                                                trash_message = del_file_counter+' files and '+del_folder_counter+' folder have been moved to Trash.'; 
                                            }else if(del_file_counter ==1  && del_folder_counter > 1){
                                                trash_message = del_file_counter+' file and '+del_folder_counter+' folders have been moved to Trash.'; 
                                            }else{
                                                trash_message = del_file_counter+' files and  '+del_folder_counter+' folders have been moved to Trash.';
                                            }
                                        }else{
                                            trash_message = 'The files/folders have been trashed completely.';
                                        }
                                        Workport.utils.alertMsg('Confirmation alert',trash_message,function(){});
                                        drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                        if(list_length == 0){
                                            var button_id = $('.drive_more').attr('id');
                                            if(button_id == 'recent'){
                                                $('#drive_min_panel_div').append('<div id="drive_nofile" class="center">No recent activity.</div>');
                                                $('#drive_nofile').css('display','block');
                                            }else if(button_id == 'starred'){
                                                $('#drive_min_panel_div').append('<div id="drive_nofile" class="center">Starred files and folders will appear here.</div>');
                                                $('#drive_nofile').css('display','block');
                                            }else{
                                                $('#drive_min_panel_div').append('<div id="drive_nofile" class="center">This folder is empty. You can use the upload button on top of this panel to upload files.</div>');
                                                $('#drive_nofile').css('display','block');
                                            }
                                        }else{
                                            drive.remove_border_bottom();
                                        }
                                    }
                                })
                            }
                        });
                    }},
                    {name:'No',class:'btn no', css:{}, action:function(e){
                        drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                        return;
                    }}
                ]);
             }
        }

        function deletefile() {
             var counter=0;
             var del_file_counter=0;
             var del_folder_counter=0;
             var total_len = 0;
             var folder_len = 0;
             var file_len =0;
             var message = '';
             var delete_message = '';
             clearTimeout(drive_auto_sync);
             $('#drive_min_panel_div').find(":checkbox").each(function(){
                if(this.checked == true){
                    counter++;
                    if(this.name == 'application/vnd.google-apps.folder'){
                        folder_len++;
                    }else{
                        file_len++;
                    }
                }
             });
             if(file_len == 1 && folder_len == 0){
                message = 'Are you sure you want to permanently delete the file/folder?</br><b>Warning: You cannot undo this action.</b>';
             }else if(file_len == 0 && folder_len == 1){
                message = 'Are you sure you want to permanently delete the file/folder?</br><b>Warning: You cannot undo this action.</b>';
             }else if(file_len == 1 && folder_len == 1){
                message = 'Are you sure you want to permanently delete 1 file and 1 folder?</br><b>Warning: You cannot undo this action.</b>';
             }else if(file_len >1 || folder_len >1){
                if(file_len > 1 && folder_len == 0){
                    message = 'Are you sure you want to permanently delete '+file_len+' files?</br><b>Warning: You cannot undo this action.</b>';
                }else if(file_len == 0 && folder_len >1 ){
                    message = 'Are you sure you want to permanently delete '+folder_len+' folders?</br><b>Warning: You cannot undo this action.</b>';
                }else if(file_len > 1 && folder_len == 1){
                    message = 'Are you sure you want to permanently delete '+file_len+' files and '+folder_len+' folder?</br><b>Warning: You cannot undo this action.</b>'; 
                }else if(file_len ==1  && folder_len > 1){
                    message = 'Are you sure you want to permanently delete '+file_len+' file and '+folder_len+' folders?</br><b>Warning: You cannot undo this action.</b>'; 
                }else{
                    message = 'Are you sure you want to permanently delete '+file_len+' files and '+folder_len+' folders?</br><b>Warning: You cannot undo this action.</b>';
                }
             }else{
                message = 'Are you sure you want to permanently delete them?</br><b>Warning: You cannot undo this action.</b>';
             }
             if(counter>0){
                Workport.utils.alertMsg('Confirmation alert',message,[
                    {name:'Yes',class:'btn yes', css:{}, action:function(e){
                        $(":checkbox").each(function(){
                            if(this.checked == true){
                                var folderid = this.id;
                                var _type= this.name;
                                Workport.Google.drive.files.delete(folderid,function(resp){
                                    if(resp.code == 404 || resp.code != undefined){
                                        console.log('not successful');
                                        total_len++;
                                        if(resp.code == '404'){
                                            if(_type == 'application/vnd.google-apps.folder'){
                                            del_folder_counter++;
                                            }else{
                                                del_file_counter++;
                                            }
                                            $('#'+folderid).remove();
                                        }
                                    }else{
                                        if(_type == 'application/vnd.google-apps.folder'){
                                            del_folder_counter++;
                                        }else{
                                            del_file_counter++;
                                        }
                                        total_len++;
                                        $('#'+folderid).remove();
                                    }
                                    if(total_len == counter){
                                        var list_length = $('.drive_contact').length;
                                        $('#drive_trash').css('opacity','0.6');
                                        $('#drive_trash').css('cursor','');
                                        $('#drive').find('input:checkbox').attr('checked', false);
                                        if(del_file_counter == 0 && del_folder_counter == 0){
                                            delete_message = '0 file/folder have been permanently deleted.';
                                        }else if(del_file_counter == 1 && del_folder_counter == 0){
                                            delete_message = 'The file/folder has been permanently deleted. ';
                                        }else if(del_file_counter == 0 && del_folder_counter == 1){
                                            delete_message = 'The file/folder has been permanently deleted. ';
                                        }else if(del_file_counter == 1 && del_folder_counter == 1){
                                            delete_message = '1 file and 1 folder have been permanently deleted.';
                                        }else if(del_file_counter >1 || del_folder_counter >1){
                                            if(del_file_counter > 1 && del_folder_counter == 0){
                                                delete_message = del_file_counter+' files have been permanently deleted.';
                                            }else if(del_file_counter == 0 && del_folder_counter >1 ){
                                                delete_message = del_folder_counter+' folders have been permanently deleted.';
                                            }else if(del_file_counter > 1 && del_folder_counter == 1){
                                                delete_message = del_file_counter+' files and '+del_folder_counter+' folder have been permanently deleted.'; 
                                            }else if(del_file_counter ==1  && del_folder_counter > 1){
                                                delete_message = del_file_counter+' file and '+del_folder_counter+' folders have been permanently deleted.'; 
                                            }else{
                                                delete_message = del_file_counter+' files and  '+del_folder_counter+' folders have been permanently deleted.';
                                            }
                                        }else{
                                            delete_message = 'The files/folders have been permanently deleted.';
                                        }
                                        if(list_length == 0){
                                            $('#drive_min_panel_div').append('<div id="drive_nofile" class="center">No files or folders in Trash.</div>');
                                            $('#drive_nofile').css('display','block');
                                        }else{
                                            drive.remove_border_bottom();
                                        }
                                        Workport.utils.alertMsg('Confirmation alert',delete_message,function(){});
                                        drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                    }
                                })
                            }
                        });
                    }},
                    {name:'No',class:'btn no', css:{}, action:function(e){
                        drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                        return;
                    }}
                ]);
             }
        }

        function removefile() {
             var counter=0;
             var del_file_counter=0;
             var del_folder_counter=0;
             var total_len = 0;
             var folder_len = 0;
             var file_len =0;
             var message = '';
             var trash_message = '';
             clearTimeout(drive_auto_sync);
             if(permissionId == undefined){
                Workport.Google.drive.files.printAbout(function(resp){
                    permissionId = resp.permissionId;
                });
             }
             $('#drive_min_panel_div').find(":checkbox").each(function(){
                if(this.checked == true){
                    counter++;
                    if(this.name == 'application/vnd.google-apps.folder'){
                        folder_len++;
                    }else{
                        file_len++;
                    }
                }
             });
             if(file_len == 1 && folder_len == 0){
                message = 'Are you sure you want to remove the file/folder?';
             }else if(file_len == 0 && folder_len == 1){
                message = 'Are you sure you want to remove the file/folder?';
             }else if(file_len == 1 && folder_len == 1){
                message = 'Are you sure you want to remove 1 file and 1 folder?';
             }else if(file_len >1 || folder_len >1){
                if(file_len > 1 && folder_len == 0){
                    message = 'Are you sure you want to remove '+file_len+' files?';
                }else if(file_len == 0 && folder_len >1 ){
                    message = 'Are you sure you want to remove '+folder_len+' folders?';
                }else if(file_len > 1 && folder_len == 1){
                    message = 'Are you sure you want to remove '+file_len+' files and '+folder_len+' folder?'; 
                }else if(file_len ==1  && folder_len > 1){
                    message = 'Are you sure you want to remove '+file_len+' file and '+folder_len+' folders?'; 
                }else{
                    message = 'Are you sure you want to remove '+file_len+' files and '+folder_len+' folders?';
                }
             }else{
                message = 'Are you sure you want to remove them ?';
             }
             if(counter>0){
                Workport.utils.alertMsg('Confirmation alert',message,[
                    {name:'Yes',class:'btn yes', css:{}, action:function(e){
                        $(":checkbox").each(function(){
                            if(this.checked == true){
                                var folderid = this.id;
                                var _type = this.name;
                                Workport.Google.drive.files.retrievePermissions(folderid,permissionId,function(resp){
                                    if(resp.code == 404 || resp.code != undefined){
                                        console.log('not successful');
                                        total_len++;
                                        if(resp.code == '404'){
                                            if(_type == 'application/vnd.google-apps.folder'){
                                                del_folder_counter++;
                                            }else{
                                                del_file_counter++;
                                            }
                                            $('#'+folderid).remove();
                                        }
                                    }else{
                                        if(_type == 'application/vnd.google-apps.folder'){
                                            del_folder_counter++;
                                        }else{
                                            del_file_counter++;
                                        }
                                        total_len++;
                                        $('#'+folderid).remove();
                                    }
                                    if(total_len == counter){
                                        var list_length = $('.drive_contact').length;
                                        $('#drive_trash').css('opacity','0.6');
                                        $('#drive_trash').css('cursor','');
                                        $('#drive').find('input:checkbox').attr('checked', false);
                                        if(del_file_counter == 0 && del_folder_counter == 0){
                                            trash_message = '0 file/folder have been remove.';
                                        }else if(del_file_counter == 1 && del_folder_counter == 0){
                                            trash_message = 'The file/folder has been removed.';
                                        }else if(del_file_counter == 0 && del_folder_counter == 1){
                                            trash_message = 'The file/folder has been removed.';
                                        }else if(del_file_counter == 1 && del_folder_counter == 1){
                                            trash_message = '1 file and 1 folder have been removed.';
                                        }else if(del_file_counter >1 || del_folder_counter >1){
                                            if(del_file_counter > 1 && del_folder_counter == 0){
                                                trash_message = del_file_counter+' files have been removed.';
                                            }else if(del_file_counter == 0 && del_folder_counter >1 ){
                                                trash_message = del_folder_counter+' folders have been removed.';
                                            }else if(del_file_counter > 1 && del_folder_counter == 1){
                                                trash_message = del_file_counter+' files and '+del_folder_counter+' folder have been removed.'; 
                                            }else if(del_file_counter ==1  && del_folder_counter > 1){
                                                trash_message = del_file_counter+' file and '+del_folder_counter+' folders have been removed.'; 
                                            }else{
                                                trash_message = del_file_counter+' files and  '+del_folder_counter+' folders have been removed.';
                                            }
                                        }else{
                                            trash_message = 'The files/folders have been removed completely.';
                                        }
                                        if(list_length == 0){
                                            $('#drive_min_panel_div').append('<div id="drive_nofile" class="center">Files and Folders, others have shared with you, will appear here.</div>');
                                            $('#drive_nofile').css('display','block');
                                        }else{
                                            drive.remove_border_bottom();
                                        }
                                        Workport.utils.alertMsg('Confirmation alert',trash_message,function(){});
                                        drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                    }
                                });
                            }
                        });
                    }},
                    {name:'No',class:'btn no', css:{}, action:function(e){
                        drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                        return;
                    }}
                ]);
             }
        }


        function recent_trash_remove_file(){
             var counter=0;
             var del_file_counter=0;
             var total_len = 0;
             var folder_len = 0;
             var file_len =0;
             var message = '';
             var trash_message = '';
             clearTimeout(drive_auto_sync);
             if(permissionId == undefined){
                Workport.Google.drive.files.printAbout(function(resp){
                    permissionId = resp.permissionId;
                });
             }
             $('#drive_min_panel_div').find(":checkbox").each(function(){
                if(this.checked == true){
                    counter++;
                }
             });
             if(counter>0){
                $(":checkbox").each(function(){
                            if(this.checked == true){
                                var folderid = this.id;
                                var _del_file_type = $('#'+this.id).find('input').attr('class');
                                if(_del_file_type == 'share'){
                                    Workport.Google.drive.files.retrievePermissions(folderid,permissionId,function(resp){
                                        if(resp.code == 404 || resp.code != undefined){
                                            console.log('not successful');
                                            total_len++;
                                            if(resp.code == 404){
                                                $('#'+folderid).remove();
                                            }
                                        }else{
                                            del_file_counter++;
                                            total_len++;
                                            $('#'+folderid).remove();
                                        }
                                        if(total_len == counter){
                                            var list_length = $('.drive_contact').length;
                                            $('#drive_trash').css('opacity','0.6');
                                            $('#drive_trash').css('cursor','');
                                            $('#drive').find('input:checkbox').attr('checked', false);
                                            if(del_file_counter == 0){
                                                var trash_message ='0 file removed'
                                            }else if(del_file_counter == 1){
                                                var trash_message ='1 file removed'
                                            }
                                            else{
                                                var trash_message = del_file_counter+' files removed'
                                            }
                                            Workport.utils.alertMsg('Confirmation alert',trash_message,function(){});
                                            drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                            if(list_length == 0){
                                                $('#drive_min_panel_div').append('<div id="drive_nofile" class="center">No recent activity.</div>');
                                                $('#drive_nofile').css('display','block');
                                            }else{
                                                drive.remove_border_bottom();
                                            }
                                        }
                                    });
                                }else{
                                    Workport.Google.drive.files.trash(folderid,function(resp){
                                        if(resp.id == folderid || resp.code == '404'){
                                            del_file_counter++;
                                            total_len++;
                                            $('#'+folderid).remove();
                                        }else{
                                            console.log('not successful');
                                            total_len++;
                                        }
                                            if(total_len == counter){
                                                var list_length = $('.drive_contact').length;
                                                $('#drive_trash').css('opacity','0.6');
                                                $('#drive_trash').css('cursor','');
                                                $('#drive').find('input:checkbox').attr('checked', false);
                                                if(del_file_counter == 0){
                                                    var trash_message ='0 file removed'
                                                }else if(del_file_counter == 1){
                                                    var trash_message ='1 file removed'
                                                }
                                                else{
                                                    var trash_message = del_file_counter+' files removed'
                                                }
                                                Workport.utils.alertMsg('Confirmation alert',trash_message,function(){});
                                                drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                                if(list_length == 0){
                                                    $('#drive_min_panel_div').append('<div id="drive_nofile" class="center">No recent activity.</div>');
                                                    $('#drive_nofile').css('display','block');
                                                }else{
                                                    drive.remove_border_bottom();
                                                }
                                            }
                                    });
                                }
                            }
                        });
             }
        }

      function uploadFile(evt) {
        clearTimeout(drive_auto_sync);
        gapi.client.load('drive', 'v2', function() {
            var _file_len = evt.target.files.length;
            var _counter = 0;
            var file = evt.target.files[0];
            function insert(){
                insertFile(file,function(resp){
                    if(resp.error){
                        if(resp.error.code == '400'){
                            Workport.utils.alertMsg('Confirmation alert',"Upload failure.This folder is not writable.",function(){});
                        }else{
                            Workport.utils.alertMsg('Confirmation alert',resp.error.message,function(){});
                        }
                        $('#filePicker').val("");
                        $('.common_loading_text').text('Loading');
                        if(useragent == 'ie10' || useragent == 'ie11'){
                            $('#filePicker').remove();
                            $('#drive_upload').after('<input id="filePicker" type="file" title="Upload" multiple />');
                            var drive_upload = document.getElementById('filePicker');
                            drive_upload.onchange = uploadFile;
                        }
                        commLoader.hide();
                        return
                    }
                    _counter++
                    if(_counter != _file_len){
                        file = evt.target.files[_counter];
                        insert();
                    }else{
                        var _folderlist_data = [];
                        var _listdata = [];
                        var listid = $('.drive_more').attr('id');
                        if(listid == undefined || listid == 'trash' || listid == 'starred' || listid == ''|| listid == 'recent'|| listid == 'sharedWithMe' || listid =='search_result'){
                            var upload_message = "";
                            if(_counter==1){
                                upload_message = "1 file has been uploaded"
                            }else{
                                upload_message = _counter +" files have been uploaded"
                            }
                            Workport.utils.alertMsg('Confirmation alert',upload_message,function(){});
                            commLoader.hide();
                            drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                            return
                        }
                        else{
                            Workport.Google.drive.files.listFullfolderfiles({maxResults:10000,'q':'mimeType = "application/vnd.google-apps.folder" and trashed = false'},listid,function(resp,nextPageToken){
                                for(i in resp){
                                        if(resp[i].parents[0] != undefined){
                                            _folderlist_data.push(resp[i]);
                                        } 
                                }
                                _folderlist_data = Workport.Google.drive.files.listsort(_folderlist_data);
                                Workport.Google.drive.files.listFullfolderfiles({maxResults:70,'q':'mimeType != "application/vnd.google-apps.folder" and trashed = false'},listid,function(resp,nextPageToken){
                                    for(i in resp){
                                        if(resp[i].parents[0] != undefined){
                                            _listdata.push(resp[i]);
                                        } 
                                    }
                                    if(_folderlist_data.length == 0 && _listdata.length ==0){
                                        var viewModel = drivelist([],undefined);
                                        kendo.bind($("#drive_min_panel_div"), viewModel);
                                        $('#drive_min_panel_div').append('<div id="drive_nofile" class="center">This folder is empty. You can use the upload button on top of this panel to upload files.</div>');
                                        $('.drive_panel_functions').css('display','block');
                                        $('#drive_nofile').css('display','block');
                                        commLoader.hide();
                                        $('#filePicker').val("");
                                        if(useragent == 'ie10' || useragent == 'ie11'){
                                            $('#filePicker').remove();
                                            $('#drive_upload').after('<input id="filePicker" type="file" title="Upload" multiple />');
                                            var drive_upload = document.getElementById('filePicker');
                                            drive_upload.onchange = uploadFile;
                                        }
                                        $('.common_loading_text').text('Loading');
                                        $('.drive_more').attr('id',listid);
                                        $('.drive_more').attr('disabled','disabled');
                                        drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                    }else{
                                        _listdata = Workport.Google.drive.files.listsort(_listdata);
                                        _folderlist_data = _folderlist_data.concat(_listdata);
                                        var viewModel = drivelist(_folderlist_data,nextPageToken);
                                        $('.drive_panel_functions').css('display','block');
                                        kendo.bind($("#drive_min_panel_div"), viewModel);
                                        commLoader.hide();
                                        $('#filePicker').val("");
                                        if(useragent == 'ie10' || useragent == 'ie11'){
                                            $('#filePicker').remove();
                                            $('#drive_upload').after('<input id="filePicker" type="file" title="Upload" multiple />');
                                            var drive_upload = document.getElementById('filePicker');
                                            drive_upload.onchange = uploadFile;
                                        }
                                        $('.common_loading_text').text('Loading');
                                        drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                        var list_length = _folderlist_data.length;
                                        if(list_length > 0 ){
                                            drive.remove_border_bottom();
                                        }
                                        $('.drive_more').attr('id',listid);
                                        if(nextPageToken == undefined){
                                           $('.drive_more').attr('disabled','disabled'); 
                                        }
                                    }
                                    var upload_message = "";
                                    if(_counter == 1){
                                        upload_message = "1 file has been uploaded"
                                    }else{
                                        upload_message = _counter +" files have been uploaded"
                                    }
                                    Workport.utils.alertMsg('Confirmation alert',upload_message,function(){});
                                });
                            });
                        }
                    }
                })
            }
            insert();
        });
    }

      function insertFile(fileData, callback) {
        var file_id = $('.drive_more').attr('id');
        if(file_id == undefined || file_id == 'trash' || file_id == 'starred' || file_id == '' || file_id =='sharedWithMe' || file_id == 'recent' || file_id =='search_result'){
            file_id = rootId;
        }
        commLoader.setOptions(loader_bg_transparent);
        commLoader.show();
        $('.common_loading_text').text('Uploading');
        var boundary = '-------314159265358979323846';
        var delimiter = "\r\n--" + boundary + "\r\n";
        var close_delim = "\r\n--" + boundary + "--";
        var reader = new FileReader();
        if(useragent == 'ie11' || useragent == 'ie10'){
            reader.readAsDataURL(fileData);
        }else{
            reader.readAsBinaryString(fileData);
        }
        reader.onload = function(e) {
          var contentType = fileData.type || 'application/octet-stream';
          
          var metadata = {
            'title': fileData.name,
            'mimeType': contentType,
            'parents':[{
                "id":file_id
            }]            
          };    
          if(useragent == 'ie11' || useragent == 'ie10'){
             var base64Data = reader.result.split('base64,')[1];
          }else{
             var base64Data = btoa(reader.result);
          }
          var multipartRequestBody =
              delimiter +
              'Content-Type: application/json\r\n\r\n' +
              JSON.stringify(metadata) +
              delimiter +
              'Content-Type: ' + contentType + '\r\n' +
              'Content-Transfer-Encoding: base64\r\n' +
              '\r\n' +
              base64Data +
              close_delim;
          var request = gapi.client.request({
              'path': '/upload/drive/v2/files',
              'method': 'POST',
              'params': {'uploadType': 'multipart'},
              'headers': {
                'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
              },
              'body': multipartRequestBody
          });
          request.execute(callback);
        }
      }

		return{
			init:function(){
				pageTpl.appendTo("#drive");
                $('.drive_panel_functions').hide();
                if(useragent == 'safari_windows' || useragent == 'safari_mac'){
                    $('#filePicker').css('display','block');
                }
                $('#drive_min_panel_div').slimScroll({
                            height: (height-60)+'px',
                            disableFadeOut:true,
                            wheelStep: 20,
                            touchScrollStep: 200,
                            allowPageScroll: true,
                            width: '100%',
                            start: 'top'
                }).bind('slimscroll', function(e, pos){
                    if (pos == 'bottom') {
                        $('.drive_more').trigger('click');
                    }
                    if(pos == 'top') {
                        $('.drive_sync').trigger('click');
                    }
                });
                $('#drive_max_panel_div').slimScroll({
                            height: (height-101)+'px',
                            disableFadeOut:true,
                            width:'768px'
                });

	            $('.drive_panel_resize').bind({
	                click: function (e) {
                        var title = $(".drive_panel_resize").attr("title");
                        if(title=='Expand'){
                            $(".drive_panel_resize").attr("title",'Collapse');
                        }else{
                            $(".drive_panel_resize").attr("title",'Expand');
                        }
	                    HomePage_Scrollable.zoom(WPMoudleManager.getModule('drive').index);     
	            }});
                
                HomePage_Scrollable.registerZoomCallback(WPMoudleManager.getModule('drive').index,function () {
                        $(".drive_panel_resize").toggleClass("main_common_f_13_13 main_common_m_13_13");
                        Workport.Communication.sendReport('GDrive');
                        $('#drive_authorize').css('top','17px');
                        $('#drive_authorize').css('left','-157px');
                        $('#commloader_position').append('#drive > .up{top: 45px;}');
                        $('#google_drive_text').css('color','black');
                        $('#commloader_position').append(' #drive_nofile{position: absolute;display: none;width: 90%;line-height: 20px;font-size: 16px;text-align: center;top: 45%;}');
                        if(typeof(gapi) == 'undefined'){
                            drive.timeoutMessage();
                            return;
                        }
                        if(rootId != undefined){
                            clearTimeout(drive_auto_sync);
                            $('#drive_search_min').css('display','none');
                            $('#drive_search_max_panel').css('display','block');
                            $('#drive_filterMenu').hide();
                            $('.drive_panel_functions').hide();
                            $('#drive_trash').css('opacity','0.6');
                            $('#drive_trash').css('cursor','');
                            commLoader.setOptions(loader_bg_transparent);
                            commLoader.show();
                            minPageTmpl.empty();
                            var folderlist_data = [];
                            var listdata = [];
                            Workport.Google.drive.files.listFullfolderfiles({maxResults:10000,'q':'mimeType = "application/vnd.google-apps.folder" and trashed = false'},rootId,function(resp,nextPageToken){
                                for(i in resp){
                                        if(resp[i].parents[0] != undefined){
                                            folderlist_data.push(resp[i]);
                                        } 
                                }
                                folderlist_data = Workport.Google.drive.files.listsort(folderlist_data);
                                Workport.Google.drive.files.listFullfolderfiles({maxResults:70,'q':'mimeType != "application/vnd.google-apps.folder" and trashed = false'},rootId,function(resp,nextPageToken){
                                    for(i in resp){
                                        if(resp[i].parents[0] != undefined){
                                            listdata.push(resp[i]);
                                        } 
                                    }
                                    listdata = Workport.Google.drive.files.listsort(listdata);
                                    folderlist_data = folderlist_data.concat(listdata);
                                    if(folderlist_data.length == 0){
                                        var viewModel = drivelist([],undefined);
                                        kendo.bind($("#drive_min_panel_div"), viewModel);
                                        commLoader.hide();
                                        $('#drive_min_panel_div').append('<div id="drive_nofile" class="center">This folder is empty. You can use the upload button on top of this panel to upload files.</div>');
                                        $('.drive_panel_functions').css('display','block');
                                        $('#drive_nofile').css('display','block');
                                        $('#drive_filterMenu_button').show();
                                        drive.drive_resize();
                                    }else{
                                        $('#drive_filterMenu_button').show();
                                        drive.drive_resize();
                                        var viewModel = drivelist(folderlist_data,nextPageToken);
                                        $('.drive_panel_functions').css('display','block');
                                        kendo.bind($("#drive_min_panel_div"), viewModel);
                                        commLoader.hide();
                                        var list_length = folderlist_data.length;
                                        if(list_length > 0){
                                            drive.remove_border_bottom();
                                        }
                                    }
                                    drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                    $('#drive_googleDrive').addClass('drive_googleDrive_filePicker_position');
                                    $('#filePicker').addClass('drive_googleDrive_filePicker_position');
                                    $('#drive_trash').addClass('drive_trash_upload_position');
                                    $('#drive_upload').addClass('drive_trash_upload_position');
                                    $('.drive_panel_functions').show();
                                    $('#folder_path').css('display','block');
                                    $('#google_drive_text').css('display','block');
                                    $('#google_drive_text').removeClass('mouse_pointer');
                                    $('.drive_panel_functions').css('height','70px');
                                    $('#my_drive_button').addClass('bg_gray');
                                    $('#my_drive_button').addClass('drive_path');
                                    $('#google_drive_text').text("My Drive");
                                    $('#folder_path').empty();
                                    $('#my_drive_button').attr('disabled','disabled');
                                    $('#my_drive_button').siblings().removeClass('bg_gray');
                                    $('#my_drive_button').siblings().removeClass('drive_path');
                                    $('#my_drive_button').siblings().attr('disabled',false);
                                    $('.drive_more').attr('id',rootId);
                                    if(nextPageToken == undefined){
                                        $('.drive_more').attr('disabled','disabled'); 
                                    }
                                });
                            });
                            $('#google_drive_text').click(function(){
                                clearTimeout(drive_auto_sync);
                                var moreId = $('.drive_more').attr('id');
                                var _text = $('#google_drive_text').text();
                                $('#google_drive_text').css('color','black');
                                if(_text == 'Back to all'){
                                    var _path = $('.drive_path').attr('id');
                                    if(_path == 'share_with_me_button'){
                                        $('#google_drive_text').text('Shared with Me');
                                        $('#search_max_input').val("");
                                        $('#google_drive_text').css('color','black');
                                        $('#share_with_me_button').addClass('bg_gray');
                                        $('#share_with_me_button').siblings().removeClass('bg_gray');
                                        $('#folder_path').empty();
                                        $('.\\.v2').trigger('click');
                                        $('#google_drive_text').removeClass('mouse_pointer');
                                    }else if(_path == 'receen_button'){
                                        $('#google_drive_text').text('Recent');
                                        $('#search_max_input').val("");
                                        $('#google_drive_text').css('color','black');
                                        $('#receen_button').addClass('bg_gray');
                                        $('#receen_button').siblings().removeClass('bg_gray');
                                        $('#folder_path').empty();
                                        $('.\\.v3').trigger('click');
                                        $('#google_drive_text').removeClass('mouse_pointer');
                                    }else if(_path == 'starred_button'){
                                        $('#google_drive_text').text('Starred');
                                        $('#search_max_input').val("");
                                        $('#google_drive_text').css('color','black');
                                        $('#starred_button').addClass('bg_gray');
                                        $('#starred_button').siblings().removeClass('bg_gray');
                                        $('#folder_path').empty();
                                        $('.\\.v4').trigger('click');
                                        $('#google_drive_text').removeClass('mouse_pointer');
                                    }else if(_path == 'trash_button'){
                                        $('#google_drive_text').text('Trash');
                                        $('#search_max_input').val("");
                                        $('#google_drive_text').css('color','black');
                                        $('#trash_button').addClass('bg_gray');
                                        $('#trash_button').siblings().removeClass('bg_gray');
                                        $('#folder_path').empty();
                                        $('.\\.v5').trigger('click');
                                        $('#google_drive_text').removeClass('mouse_pointer');
                                    }else{
                                        $('#google_drive_text').text('My Drive');
                                        $('#search_max_input').val("");
                                        $('#google_drive_text').css('color','black');
                                        $('#my_drive_button').addClass('bg_gray');
                                        $('#my_drive_button').siblings().removeClass('bg_gray');
                                        $('#folder_path').empty();
                                        $('.\\.v0').trigger('click');
                                        $('#google_drive_text').removeClass('mouse_pointer');
                                    }
                                    return
                                }
                                if(moreId == rootId || moreId =='sharedWithMe' || moreId =='recent' || moreId =='starred' || moreId =='trash' ){
                                    return
                                }
                                $('#folder_path').empty();
                                if(_text == 'My Drive'){
                                    $('.\\.v0').trigger('click');
                                    $('#google_drive_text').removeClass('mouse_pointer');
                                }else if(_text == "Shared with Me"){
                                    $('.\\.v2').trigger('click');
                                    $('#google_drive_text').removeClass('mouse_pointer');
                                }else if(_text == 'Recent'){
                                    $('.\\.v3').trigger('click');
                                    $('#google_drive_text').removeClass('mouse_pointer');
                                }else if(_text == 'Starred'){
                                    $('.\\.v4').trigger('click');
                                    $('#google_drive_text').removeClass('mouse_pointer');
                                }else if(_text == 'Trash'){
                                    $('.\\.v5').trigger('click');
                                    $('#google_drive_text').removeClass('mouse_pointer');
                                }else{
                                    commLoader.setOptions(loader_bg_transparent);
                                    commLoader.show();
                                    minPageTmpl.empty();
                                    drive.loaddata();
                                }
                                if(useragent == 'ie10' || useragent == 'ie11'){

                                }else{
                                    event.stopPropagation();
                                }
                            })
                            
                            $('#folder_path').click(function(event){
                                var folderid = event.target.id;
                                if(folderid == "folder_path" || folderid == "" ){
                                    return ;
                                }
                                var pageid = $('.drive_more').attr('id');
                                if(folderid != pageid){
                                    minPageTmpl.empty();
                                    clearTimeout(drive_auto_sync);
                                    commLoader.setOptions(loader_bg_transparent);
                                    commLoader.show();
                                    $('#'+folderid).nextAll('span').remove();
                                    $('#'+folderid).removeClass('textUndreline');
                                    Workport.Google.drive.files.listFullfolderfiles({maxResults:10000,'q':'mimeType = "application/vnd.google-apps.folder" and trashed = false'},folderid,function(resp,nextPageToken){
                                        var folderlist_data = [];
                                        var listdata = [];
                                        for(i in resp){
                                                if(resp[i].parents[0] != undefined){
                                                    folderlist_data.push(resp[i]);
                                                } 
                                        }
                                        folderlist_data = Workport.Google.drive.files.listsort(folderlist_data);
                                        Workport.Google.drive.files.listFullfolderfiles({maxResults:70,'q':'mimeType != "application/vnd.google-apps.folder" and trashed = false'},folderid,function(resp,nextPageToken){
                                            for(i in resp){
                                                if(resp[i].parents[0] != undefined){
                                                    listdata.push(resp[i]);
                                                } 
                                            }
                                            listdata = Workport.Google.drive.files.listsort(listdata);
                                            folderlist_data = folderlist_data.concat(listdata)
                                            var viewModel = drivelist(folderlist_data,nextPageToken);
                                            kendo.bind($("#drive_min_panel_div"), viewModel);
                                            commLoader.hide();
                                            drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                            var list_length = folderlist_data.length;
                                            if(list_length > 0 ){
                                                drive.remove_border_bottom();
                                            }
                                            $('.drive_more').attr('id',folderid);
                                            if(nextPageToken == undefined){
                                               $('.drive_more').attr('disabled','disabled'); 
                                            }
                                        });
                                    });

                                }
                                if(useragent == 'ie10' || useragent == 'ie11'){

                                }else{
                                    event.stopPropagation();
                                }
                            })
                        }
                    },
                    function () {
                        $('#drive_authorize').css('top','2px');
                        $('#drive_authorize').css('left','0px');
                        $(".drive_panel_resize").toggleClass("main_common_f_13_13 main_common_m_13_13");
                        $('#search_max_input').val("");
                        $(".drive_panel_resize").attr("title",'Expand');
                        $('#commloader_position').empty();
                        $('#google_drive_text').css('color','black');
                        
                        if(typeof(gapi) == 'undefined'){
                            drive.timeoutMessage();
                            return
                        }
                        if(rootId != undefined){
                            $('#drive_search_min').css('display','block');
                            $('#drive_search_max_panel').css('display','none');
                            $('#drive_trash').css('opacity','0.6');
                            $('#drive_trash').css('cursor','');
                            commLoader.setOptions(loader_bg_transparent);
                            commLoader.show();
                            minPageTmpl.empty();
                            //Workport.Communication.sendReport('drive');
                            clearTimeout(drive_auto_sync);
                            $('.drive_panel_functions').hide();
                            $('#drive_filterMenu_button').hide();
                            drive.drive_resize();
                            $('#drive_googleDrive').removeClass('drive_googleDrive_filePicker_position');
                            $('#filePicker').removeClass('drive_googleDrive_filePicker_position');
                            $('#drive_trash').removeClass('drive_trash_upload_position');
                            $('#drive_upload').removeClass('drive_trash_upload_position');
                            $('#folder_path').css('display','none');
                            $('#google_drive_text').css('display','none');
                            $('.drive_panel_functions').css('height','26px');
                            Workport.Google.drive.files.listFullfolderfiles({maxResults:10000,'q':'mimeType = "application/vnd.google-apps.folder" and trashed = false'},rootId,function(resp,nextPageToken){
                                var folderlist_data = [];
                                var listdata = [];
                                for(i in resp){
                                        if(resp[i].parents[0] != undefined){
                                            folderlist_data.push(resp[i]);
                                        } 
                                }
                                folderlist_data = Workport.Google.drive.files.listsort(folderlist_data);
                                Workport.Google.drive.files.listFullfolderfiles({maxResults:30,'q':'mimeType != "application/vnd.google-apps.folder" and trashed = false'},rootId,function(resp,nextPageToken){
                                    for(i in resp){
                                        if(resp[i].parents[0] != undefined){
                                            listdata.push(resp[i]);
                                        } 
                                    }
                                    listdata = Workport.Google.drive.files.listsort(listdata);
                                    folderlist_data = folderlist_data.concat(listdata);
                                    if(folderlist_data.length == 0){
                                        var viewModel = drivelist([],undefined);
                                        kendo.bind($("#drive_min_panel_div"), viewModel);
                                        $('#drive_min_panel_div').append('<div id="drive_nofile" class="center">This folder is empty. You can use the upload button on top of this panel to upload files.</div>');
                                        $('#drive_nofile').css('display','block');
                                        commLoader.hide();
                                        drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                    }else{
                                          var viewModel = drivelist(folderlist_data,nextPageToken);
                                          $('.drive_panel_functions').css('display','block');
                                          kendo.bind($("#drive_min_panel_div"), viewModel);
                                          commLoader.hide();
                                          drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                          if(folderlist_data.length > 0 ){
                                              var _lastItem = folderlist_data[folderlist_data.length-1].id;
                                              $("#"+_lastItem).css('border','none');
                                          }
                                    }
                                   
                                    $('.drive_more').attr('id',rootId);
                                    $('.drive_fixedwidth').text('My Drive');
                                    $('.drive_fixedwidth').append('<span class="k-icon k-i-arrow-s drive_filter_icon right"></span>');
                                    $('#drive_filterMenu').show();
                                    $('.drive_panel_functions').show();
                                    if(nextPageToken == undefined){
                                       $('.drive_more').attr('disabled','disabled'); 
                                    }
                                    if(useragent == 'firefox'){
                                        $('.drive_filter_icon').css('top','-8px');
                                    }
                                });
                            });
                        }
                    }
                );
                commLoader.setOptions(loader_bg_transparent);
                commLoader.show();
	            wp_drive_filterMenu();
                if(typeof(gapi) == 'undefined'){
                    drive.timeoutMessage();
                    return;
                }
                Workport.Google.checkAuth(function (result) {
                    if (!result.error) {
                        minPageTmpl.empty();
                        commLoader.setOptions(loader_bg_transparent);
                        commLoader.show();
                        drive.loaddata();                      
                    }else{
                        $('#login_div').css('display','block');
                        commLoader.hide();
                    }
                });

                minPageTmpl.find('#drive_authorize').click(function(e){
                    Workport.Google.authorize(false, function(result){
                        if(!result || !result.error){
                            minPageTmpl.empty();
                            commLoader.setOptions(loader_bg_transparent);
                            commLoader.show();
                            drive.loaddata();    
                        }else{
                            $('#login_div').css('display','block');
                            commLoader.hide();
                        }
                    });
                });
                
                $('#drive_upload').click(function(){
                     $('#filePicker').trigger('click');
                })
                var drive_upload = document.getElementById('filePicker');
                drive_upload.onchange = uploadFile;
                
                $('#drive_trash').click(function(){
                    var button_path = $('.drive_more').attr('id');
                    var button_path_sync = $('.drive_sync').attr('id');
                    if(button_path == 'trash'){
                        deletefile();
                    }else if( button_path =='sharedWithMe' || button_path_sync == 'sharedWithMe' ){
                        removefile();
                    }else if(button_path =='recent'){
                        recent_trash_remove_file();
                    }else{
                        trashfile(); 
                    }  
                });

                $('#drive_googleDrive').click(function(){
                    window.open('https://drive.google.com/');
                });
                $('#my_drive_button').click(function(){
                    var id = $('#my_drive_button');
                    $('.\\.v0').trigger('click');
                    $('#google_drive_text').text('My Drive');
                    drive.general_button_change(id);
                });
                $('#share_with_me_button').click(function(){
                    var id = $('#share_with_me_button');
                    $('.\\.v2').trigger('click');
                    $('#google_drive_text').text('Shared with Me');
                    drive.general_button_change(id);
                });
                $('#receen_button').click(function(){
                    var id = $('#receen_button');
                    $('.\\.v3').trigger('click');
                    $('#google_drive_text').text('Recent');
                    drive.general_button_change(id);
                });
                $('#starred_button').click(function(){
                    var id = $('#starred_button');
                    $('.\\.v4').trigger('click');
                    $('#google_drive_text').text('Starred');
                    drive.general_button_change(id);
                });
                $('#trash_button').click(function(){
                    var id = $('#trash_button');
                    $('.\\.v5').trigger('click');
                    $('#google_drive_text').text('Trash');
                    drive.general_button_change(id);
                });
                $('#drive_search_min').click(function(){
                    $('.drive_panel_resize').trigger('click');
                    $('#drive_search_min').css('display','none');
                });
                $('#search_max_input_button').click(function(){
                    drive.search_result();
                });

                $(window).resize(this.drive_resize);
            },
            loaddata:function(){
                Workport.Google.load('drive','v2',function(){
                    var listdata = [];
                    var folderlist_data = [];
                    Workport.Google.drive.files.printAbout(function(resp){
                        if(resp.error){
                            minPageTmpl.empty();
                            $('#drive_min_panel_div').append('<div id="timeout_message" class="center">'+resp.error.message+'</div>');
                            commLoader.hide();
                            return
                        }
                        rootId = resp.rootFolderId;
                        permissionId = resp.permissionId;
                        Workport.Google.drive.files.listFullfolderfiles({maxResults:10000,'q':'mimeType = "application/vnd.google-apps.folder" and trashed = false'},rootId,function(resp,nextPageToken){
                                for(i in resp){
                                        if(resp[i].parents[0] != undefined){
                                            folderlist_data.push(resp[i]);
                                        } 
                                }
                                folderlist_data = Workport.Google.drive.files.listsort(folderlist_data);
                                Workport.Google.drive.files.listFullfolderfiles({maxResults:30,'q':'mimeType != "application/vnd.google-apps.folder" and trashed = false'},rootId,function(resp,nextPageToken){
                                    for(i in resp){
                                        if(resp[i].parents[0] != undefined){
                                            listdata.push(resp[i]);
                                        } 
                                    }
                                    if(folderlist_data.length == 0 && listdata.length == 0){
                                        var viewModel = drivelist([],undefined);
                                        kendo.bind($("#drive_min_panel_div"), viewModel);
                                        $('#drive_min_panel_div').append('<div id="drive_nofile" class="center">This folder is empty. You can use the upload button on top of this panel to upload files.</div>');
                                        $('.drive_panel_functions').css('display','block');
                                        $('#drive_nofile').css('display','block');
                                        commLoader.hide();
                                        $('.drive_more').attr('id',rootId);
                                        $('.drive_more').attr('disabled','disabled');
                                        drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                    }else{
                                        listdata = Workport.Google.drive.files.listsort(listdata);
                                        folderlist_data = folderlist_data.concat(listdata);
                                        var viewModel = drivelist(folderlist_data,nextPageToken);
                                        $('.drive_panel_functions').css('display','block');
                                        drive.drive_resize();
                                        kendo.bind($("#drive_min_panel_div"), viewModel);
                                        commLoader.hide();
                                        drive_auto_sync = setTimeout(function(){$('.drive_sync').trigger('click');commLoader.hide();},DRIVE_SYNC_INTERVAL);
                                        drive.remove_border_bottom();
                                        $('.drive_more').attr('id',rootId);
                                        if(nextPageToken == undefined){
                                           $('.drive_more').attr('disabled','disabled'); 
                                        }
                                    }
                                });
                        });
                    });    
                });
            },
            list: function (parm, callback) {
                commLoader.setOptions(loader_bottom);
                commLoader.show();
                Workport.Google.drive.files.listFull(parm, function (data, nextPageToken) {
                    commLoader.hide();
                    callback(data, nextPageToken);
                });
            },
            getMore: function (nextPageToken, callback) {
                this.list({
                    pageToken: nextPageToken
                }, function (data, nextPageToken) {
                    callback(data, nextPageToken);
                });
            },
            getMoreSharedWithMe: function (nextPageToken, callback) {
                this.list({
                    pageToken: nextPageToken,
                    q:'sharedWithMe=true'
                }, function (data, nextPageToken) {
                    callback(data, nextPageToken);
                });
            },
            getMoreStarred: function (nextPageToken, callback) {
                this.list({
                    pageToken: nextPageToken,
                    q:'starred=true'
                }, function (data, nextPageToken) {
                    callback(data, nextPageToken);
                });
            },
            listfolderfile: function (parm,folderid,callback) {
                commLoader.setOptions(loader_bottom);
                commLoader.show();
                Workport.Google.drive.files.listFullfolderfiles(parm,folderid,function (data, nextPageToken) {
                    commLoader.hide();
                    callback(data, nextPageToken);
                });
            },
            getMorefolderfile: function (nextPageToken,folderid,callback) {
                this.listfolderfile({
                    pageToken: nextPageToken,
                    q:'mimeType != "application/vnd.google-apps.folder" and trashed = false'
                },folderid,function (data, nextPageToken) {
                    callback(data, nextPageToken);
                });
            },
            drive_resize:function(){
                var height = $(window).height();
                var _select_window_status = $('#drive_filterMenu_button').css('display');
                if(_select_window_status == 'block'){
                    var listDomHight = height - 101;
                }else{
                    var listDomHight = height - 117;
                }

                $('#driveTpl').find('.slimScrollDiv').height(listDomHight);
                minPageTmpl.height(listDomHight)
            },
            general_button_change:function(id){
                $('#folder_path').empty();
                $('#google_drive_text').css('color','black');
                id.addClass('bg_gray');
                id.addClass('drive_path');
                id.attr('disabled','disabled');
                id.siblings().removeClass('bg_gray');
                id.siblings().removeClass('drive_path');
                id.siblings().attr('disabled',false);
                $('#google_drive_text').removeClass('mouse_pointer');
            },
            remove_border_bottom:function(){
                var list_length = $('.drive_contact').length;
                var _select_window_status = $('#drive_filterMenu_button').css('display');
                $('.drive_contact').css('border-bottom','solid 1px white');
                if(_select_window_status == 'block'){
                    if(list_length % 3 == 1 ){
                        $('.drive_contact').eq(-1).css('border-bottom','none');
                    }else if(list_length % 3 == 2 ){
                        $('.drive_contact').eq(-1).css('border-bottom','none');
                        $('.drive_contact').eq(-2).css('border-bottom','none');
                    }else if(list_length % 3 == 0 ){
                        $('.drive_contact').eq(-1).css('border-bottom','none');
                        $('.drive_contact').eq(-2).css('border-bottom','none');
                        $('.drive_contact').eq(-3).css('border-bottom','none');
                    }   
                }else{
                    $('.drive_contact').eq(-1).css('border-bottom','none');
                }
            },
            search_result:function(){
                var search_text = $('#search_max_input').val();
                search_text = $.trim(search_text);
                clearTimeout(drive_auto_sync);
                commLoader.setOptions(loader_bg_transparent);
                commLoader.show();
                minPageTmpl.empty();
                $('#google_drive_text').text('Back to all');
                $('#google_drive_text').css('color','blue');
                $('#folder_path').empty();
                Workport.Google.drive.files.searchFullText({'q':'fullText contains '+"'"+search_text+"'"},function (resp) {
                    if(resp.error){
                        $('.bg_gray').attr('disabled',false);
                        $('.bg_gray').removeClass('bg_gray');
                        var viewModel = drivelist([],undefined);
                        kendo.bind($("#drive_min_panel_div"), viewModel);
                        $('#drive_min_panel_div').append('<div id="drive_nofile" class="center"></div>');
                        if(resp.error.code == '400'){
                            $('#drive_nofile').text('None of your files or folders matched this search.');
                        }else{
                            $('#drive_nofile').text(resp.message);
                        }
                        commLoader.hide();
                        $('#drive_nofile').show();
                        $('.drive_more').attr('id','search_result');
                        $('.drive_more').attr('disabled','disabled');
                        $('#google_drive_text').addClass('mouse_pointer');
                        return
                    }
                    var _length = resp.items.length;
                    if(resp.items.length == 0){
                        $('#folder_path').append('<span class="symbol"> > </span>')
                        $('#folder_path').append('<p class="search_result_text">' + _length+' Result for '+search_text+ '</p>');
                        $('#google_drive_text').addClass('mouse_pointer');
                        $('.bg_gray').attr('disabled',false);
                        $('.bg_gray').removeClass('bg_gray');
                        var viewModel = drivelist([],undefined);
                        kendo.bind($("#drive_min_panel_div"), viewModel);
                        //var _error_message = search_text;
                        //$('#drive_min_panel_div').append('<div id="drive_nofile" class="center">No result found for the <span id="search_no_result"></span></div>');
                        //$('#search_no_result').text(_error_message);
                        $('#drive_min_panel_div').append('<div id="drive_nofile" class="center">None of your files or folders matched this search.</div>');
                        $('#drive_nofile').css('display','block');
                        commLoader.hide();
                        $('.drive_more').attr('id','search_result');
                        $('.drive_more').attr('disabled','disabled'); 
                    }else{
                        if(search_text == ""){
                            if(_length ==1){
                                $('#folder_path').append('<span class="symbol"> > </span>')
                                $('#folder_path').append('<p class="search_result_text">' + _length+' Result</p>');
                                $('#google_drive_text').addClass('mouse_pointer');
                            }else{
                                $('#folder_path').append('<span class="symbol"> > </span>')
                                $('#folder_path').append('<p class="search_result_text">' + _length+' Results</p>');
                                $('#google_drive_text').addClass('mouse_pointer');
                            }
                        }else{
                            if(_length ==1){
                                $('#folder_path').append('<span class="symbol"> > </span>')
                                $('#folder_path').append('<p class="search_result_text">' + _length+' Result for '+search_text+ '</p>');
                                $('#google_drive_text').addClass('mouse_pointer');
                            }else{
                                $('#folder_path').append('<span class="symbol"> > </span>')
                                $('#folder_path').append('<p class="search_result_text">' + _length+' Results for '+search_text+ '</p>');
                                $('#google_drive_text').addClass('mouse_pointer');
                            }
                        }
                        $('.bg_gray').attr('disabled',false);
                        $('.bg_gray').removeClass('bg_gray');
                        var viewModel = drivelist(resp.items,undefined);
                        kendo.bind($("#drive_min_panel_div"), viewModel);
                        drive.remove_border_bottom();
                        $('.drive_file_name').highlight(search_text)
                        commLoader.hide();
                        $('.drive_more').attr('id','search_result');
                        $('.drive_more').attr('disabled','disabled');
                    }
                });
            },
            searchbtnClick:function(event){
                var keycode
                if(window.event){
                    keycode = event.keyCode;
                }else if(event.which){
                    keycode = event.which;
                }
                if(keycode==13){
                    drive.search_result();
                }
            },
            timeoutMessage:function(){
                minPageTmpl.empty();
                $('#drive_min_panel_div').append('<div id="timeout_message" class="center">'+WP_TIMEOUT_MESSAGE+'</div>');
                commLoader.hide();
            }
		}
	})();
	drive.init();
</script>